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?
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.
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.
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.
So Lambda expression is the best for development over LINQ queries.
MyList.Select(x => new { x.Id, x.Name }).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With