Using the word "let" in a LINQ query with EF 4.3

I have a simple LINQ problem that I cannot understand. I have a table Usersand a table Employees. One user can have 0 ... n employees.

I would like to do something like this:

var result = from u in context.users
             where u.UsrId = 2
             let e = u.Employees.First()
             select new 
{
  UserId = u.UsrId,
  FirstName = e.FirstName
};

This does not work, of course, because the call First()is illegal. First () can only appear at the end select. What also did not work was to select the employee in the previous query as follows:

var employee = from e. in context.Employees.....

... and then say let e = employeeinsteadlet e = u.Employees().First()

I am not sure that the use is correct. I thought this was for subqueries.

First() , Employees . , . .

+5
2
var result = from u in context.users
let e = u.Employees.FirstOrDefault(x => x.bossId == u.UsrId)
where u.UsrId = 2
select new  {   UserId = u.UsrId,   FirstName = e.FirstName }; 

tipp .

+4

, LINQ, SQL First, , . FirstOrDefault, , Top (1), , SQL. , SQL- , , , post post- .

+1

All Articles