Sleette
Sleette

Reputation: 376

Linq to Entities question

Is there a (fairly) simple explanation of why I can't do this:

var EmpList = from emp in context.Employees
              orderby emp.LastName
              select new { Name = emp.FirstName + " " + emp.LastName };

And further, is it possible to achieve this in the query or do I have to do this kind of processing after, with a foreach or something similar?

Thanks..

Upvotes: 0

Views: 73

Answers (2)

Mark Byers
Mark Byers

Reputation: 838196

You can do that.

However it seems unnecessary to create an anonymous type with only one member. Try this instead:

var employeeNames =
    from emp in context.Employees
    orderby emp.LastName
    select emp.FirstName + " " + emp.LastName;

Upvotes: 4

Justin Morgan
Justin Morgan

Reputation: 30760

As far as I know, you can do that. You can also do this:

var EmpList = from emp in context.Employees
              let name = emp.FirstName + " " + emp.LastName
              orderby emp.LastName
              select name;

Haven't tested this in Studio though.

Upvotes: 1

Related Questions