Firstly, some recommendations for your current class...
The Save
function is exceptionally poor for pulling out data from the file again. Do this instead:
public class Product {
// Note that I've added a constructor for this class - this'll help later
public Product(string productName, string customerName, string firmwareLocation) {
this.productName = productName;
this.customerName = customerName;
this.firmwareLocation = firmwareLocation;
}
public void Save (System.IO.TextWriter textOut)
{
textOut.WriteLine(String.Format(
"{0},{1},{2}", this.productName, this.customerName, this.firmwareLocation);
}
}
So instead of getting this:
...
Awesome Hairdryer
Nick Bull
C://hair.firmware
Awesome TV
Nick Bull
C://tv.firmware
...
you get this:
...
Awesome Hairdryer,Nick Bull,C://hair.firmware
Awesome TV,Nick Bull,C://tv.firmware
...
Once you've done that...
It's a really easy question. One liner, with some examples of methods used as filters for "searching", if you want them:
IEnumerable<string> lines = File.ReadLines(pathToTextFile)
.TakeWhile(line => line.Contains("Nick Bull"));
EDIT: Even neater one-liner, returning a Product
collection:
List<Product> lines = File.ReadLines(pathToTextFile)
.TakeWhile(line => line.Contains("Nick Bull"))
.Select(line => new Product(line.Split(',')[0], line.Split(',')[1], line.Split(',')[2])
.ToList();
To iterate through them and do other more complicated stuff, you could read them all then do stuff:
var lines = File.ReadAllLines(filePath);
var products = new List<Product>();
foreach (string line in lines) {
if (Regex.IsMatch(line, @"super awesome regex")) {
string[] lineItems = line.Split(','); // Splits line at commas into array
products.Add(new Product(line[0], line[1], line[2]); // Thanks to our constructor
}
}
foreach (var product in products) {
Console.WriteLine(String.Format("Product name: {0}", product.productName));
}
Search functionality update
To search, use these functions:
public enum ProductProperty {
ProductName,
CustomerName,
FirmwareLocation
}
List<Product> GetAllProductsFromFile(string filePath) {
if (!File.Exists(filePath)) throw FileNotFoundException("Couldn't find " + filePath);
return File.ReadLines(filePath)
.Select(line => new Product(line.Split(',')[0], line.Split(',')[1], line.Split(',')[2])
.ToList();
}
function SearchProductsByProperty(IEnumerable<Product> products, ProductProperty productProperty, string value) {
return products.ToList().Where(product =>
(productProperty == ProductProperty.ProductName) ? product.productName == productName :
(productProperty == ProductProperty.CustomerName) ? product.customerName == customerName :
(productProperty == ProductProperty.FirmwareName) ? product.firmwareName == firmwareName : throw new NotImplementedException("ProductProperty must be ProductProperty.ProductName, ProductProperty.CustomerName or ProductProperty.FirmwareName");
);
}
Then:
var products = GetAllProductsFromFile(filePath);
var searchedProducts = SearchProductsByProperty(products, ProductProperty.ProductName, "Awesome TV");
foreach (var product in searchedProducts) {
// Each product will have a `ProductName` equal to "Awesome TV".
// Finally, get the customer name by doing this within this foreach loop, using `product.customerName`
Console.WriteLine(product.customerName);
}