Select method in List<t> Collection Select method in List<t> Collection asp.net asp.net

Select method in List<t> Collection


Well, to start with List<T> does have the FindAll and ConvertAll methods - but the more idiomatic, modern approach is to use LINQ:

// Find all the people older than 30var query1 = list.Where(person => person.Age > 30);// Find each person's namevar query2 = list.Select(person => person.Name);

You'll need a using directive in your file to make this work:

using System.Linq;

Note that these don't use strings to express predicates and projects - they use delegates, usually created from lambda expressions as above.

If lambda expressions and LINQ are new to you, I would suggest you get a book covering LINQ first, such as LINQ in Action, Pro LINQ, C# 4 in a Nutshell or my own C# in Depth. You certainly can learn LINQ just from web tutorials, but I think it's such an important technology, it's worth taking the time to learn it thoroughly.


you can also try

var query = from p in list            where p.Age > 18            select p;


Try this:

using System.Data.Linq;var result = from i in list             where i.age > 45             select i;

Using lambda expression please use this Statement:

var result = list.where(i => i.age > 45);