0

How to write this linq query using Lambda expression

public List<Employee> GetList()
 {
     return (from c in DALContext.MST
             select new Employee(ID=c.CD, Name=c.NAME)).ToList();
 }

1 Answer 1

5

Try this:

public List<Employee> GetList()
{
    return DALContext.MST.Select(c => new Employee { ID = c.CD, Name = c.NAME }).ToList();
}
Sign up to request clarification or add additional context in comments.

3 Comments

new Employee(ID = c.CD, Name = c.NAME) should be new Employee{ID = c.CD, Name = c.NAME}
@Metro of course you're right - I was focussing on the extension method bit and totally missed the syntax error. Thanks :)
if you were using named parameters, it would be: new Employee(ID: c.CD, Name: c.NAME)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.