开发者

How to Perform an Action on results of .FindAll()

开发者 https://www.devze.com 2023-01-02 01:37 出处:网络
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 res

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));
0

精彩评论

暂无评论...
验证码 换一张
取 消