LINQ How to select more than 1 property in a lambda expression?

Tony picture Tony · Dec 3, 2010 · Viewed 39.9k times · Source

We often use the following lambda expression

MyList.Select(x => x.Id).ToList();

Is possible to get more than 1 property usinglambda expression ? E.g Id and Name from MyList?

I know that I can use the following syntax:

(from item in MyList
 select new { item.Id, item.Name }).ToList();

Can I do the same thing using lambda expression?

Answer

Anthony Pegram picture Anthony Pegram · Dec 3, 2010
MyList.Select(x => new { x.Id, x.Name }).ToList();