Trying to get my head into Linq to Objects and for the life of me it escapes me...I want to get a list of all the products where the _name starts with J.
Product prod;
for (int i = 0; i < 10;i++ ) {
prod = new Product();
prod._Name = "J" + i.ToString();
prod._Surname = "F" + i.ToString();
}
Update
Product prod;
List<Product> productList = new List<Product>();
for (int i = 0; i < 10;i++ ) {
prod = new Product();
prod._Name = "J" + i.ToString();
prod._Surname = "F" + i.ToString();
productList.Add(prod);
}
var query = productList.Where(p=> p._Name.StartsWith("J"));
Thanks Jon
From stackoverflow
-
Well you haven't given a collection of any kind in your code sample, but assuming you have a collection called
products
you want something like:var query = products.Where(prod => prod._Name.StartsWith("J"));
That will give an
IEnumerable<Product>
. If you want aList<Product>
just add a call toToList()
to the end:var query = products.Where(prod => prod._Name.StartsWith("J")) .ToList();
-
If you want to get rid of this error, juste move the
Product proc;
in your loop :for (int i = 0; i < 10;i++ ) { Product prod = new Product(); ...
0 comments:
Post a Comment