Since I still have a limited knowledge of LINQ, I figured I would ask how to simplify this action. I am trying to write a statment that selects Customers from a list and performs some action on the results.
Say I have:
public List<Customer> Customers
Customers.FindAll(delegate(Customer c) { return c.Category ==开发者_JAVA百科 "A"; });
Now say I want to take all of those customers that have Category == "A" and print their c.Names or set c.Value = "High".
Is there a quick way to accomplish this without having to place the results in another list and iterate over each one?
Using Linq Where
instead of FindAll
:
foreach (var c in Customers.Where(c => c.Category == "A"))
{
Console.WriteLine(c.Name);
c.Value = "High";
}
Should be more efficient this way, since it doesn't have to create a new list.
You can do this:
Customers.FindAll(delegate(Customer c) { return c.Category == "A"; })
.ForEach(c => Console.WriteLine(c.Name));
You can do this :
public List<Customer> Customers
Customers.FindAll(delegate(Customer c) { return c.Category == "A"; }).ForEach(c => Console.WriteLine(c.Names));
精彩评论