Using the “let” kewword in a LINQ Query with EF 4.3

点点圈 提交于 2019-12-05 11:10:45
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 }; 

Just a tipp without test.

First can be used except in the middle of a LINQ queriable statement, when translating that statement to SQL theres actually no way to constitute a First as its behavior is to throw an error. FirstOrDefault however can be translated to Top(1) which is how its transformed to SQL. This means using first in the middle of a SQL translatable statement isnt really valid, however i believe they have allowed this as the last statement as a zero result can be translated to an exception post query execution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!