tags:

views:

85

answers:

3

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 == "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?

+3  A: 

You can do this:

Customers.FindAll(delegate(Customer c) { return c.Category == "A"; })
    .ForEach(c => Console.WriteLine(c.Name));
Mark Seemann
+2  A: 

You can do this :

public List<Customer> Customers

Customers.FindAll(delegate(Customer c) { return c.Category == "A"; }).ForEach(c => Console.WriteLine(c.Names));
hoang
+4  A: 

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.

tzaman