Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 806
Tony Avatar asked Dec 03 '10 14:12

Tony


People also ask

Can a lambda statement have more than one statement?

The body of a statement lambda can consist of any number of statements; however, in practice there are typically no more than two or three.

What does => mean in LINQ?

the operator => has nothing to do with linq - it's a lambda expression. It's used to create anonymous functions, so you don't need to create a full function for every small thing.

Can you use lambda expression instead of LINQ query?

So performance-wise, there's no difference whatsoever between the two. Which one you should use is mostly personal preference, many people prefer lambda expressions because they're shorter and more concise, but personally I prefer the query syntax having worked extensively with SQL.

Which is faster LINQ or lambda?

So Lambda expression is the best for development over LINQ queries.


1 Answers

MyList.Select(x => new { x.Id, x.Name }).ToList();
like image 165
Anthony Pegram Avatar answered Sep 19 '22 17:09

Anthony Pegram