Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'select new' inside Linq lambda expression?

Tags:

lambda

linq

How do i do this in one statement vs breaking up in two?

 var newpeople= _rep.GetPeople().Where(p=>p.personID).Select(new KindoFPerson...id=p.id etc)
like image 825
zsharp Avatar asked May 22 '09 22:05

zsharp


People also ask

Does LINQ Select return new object?

While the LINQ methods always return a new collection, they don't create a new set of objects: Both the input collection (customers, in my example) and the output collection (validCustomers, in my previous example) are just sets of pointers to the same objects.

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.

What is lambda expression in LINQ?

A lambda expression is a convenient way of defining an anonymous (unnamed) function that can be passed around as a variable or as a parameter to a method call. Many LINQ methods take a function (called a delegate) as a parameter.

How do you use the operator in lambda expression?

All lambda expressions use the lambda operator =>, that can be read as “goes to” or “becomes”. The left side of the lambda operator specifies the input parameters and the right side holds an expression or a code block that works with the entry parameters.


1 Answers

var newpeople= _rep.GetPeople().Where(p=>p.personID)
    .Select(p => new KindoFPerson...id=p.id etc)

Note the new "p =>".

like image 116
mqp Avatar answered Oct 16 '22 16:10

mqp