0

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..

0

2 Answers 2

4

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; 
Sign up to request clarification or add additional context in comments.

5 Comments

Is it necessary to use IQueryable:)?
No - I changed it to var now. But sometimes it is useful to know what the type is. Especially if you want to return it as a method result.
That is not what I meant, but it doesn't matter any more:)
@Shawn: I'm not at the computer atm so I can't say exactly what the error was, I'll post later if unresolved @Mark: thanks, I'll check it out later; I just simplified for example but I was sure it was getting hung up on that.. perhaps my error is elsewhere
Thanks all. You were, of course, correct. I examined my code and realized my mistake was occurring when I was accessing the members of the anonymous type, not in the query. Thanks again..
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.