实体框架查询嵌套查询 [英] Entity Framework Query Nested Query
本文介绍了实体框架查询嵌套查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是实体框架的新手,正在尝试将以下查询转换为正确的函数调用。
I am new to the entity framework and am trying to convert the following query into the correct function calls.
Select Distinct a.nodeId FROM
(SELECT *
FROM reportContents
Where fitId = '29' and reportId =
(select max(reportId)
from reportContents
where fitId = '29')
) a Where (a.nodeId IS NOT NULL)
我知道此查询可以实现我想要的功能,但是我不确定如何将其转换为实体框架!
I know this query does what i want, however i'm not sure how to translate that into the entitiy framework!
这是我的尝试。
var prevSelectedNodes = db.reportContents.Where(
f => f.fitId == id).Select(
f => f.nodeId).Distinct().ToList();
我需要以某种方式将.Select()放在where调用中。但是,这种事情似乎不太可能
I need to somehow put a .Select() in the where call. However that kind of thing dosen't seem possible
预先感谢您!
推荐答案
因为您不能创建两个LINQ嵌套的lambda表达式。您可以通过两个请求来做到这一点:
As you can't make two LINQ nested lambda expression. You can do it with two requests :
var maxReportId = db.reportContents.Where(r => r.fitId = "29").Max(r => r.RepordId);
var result = db.reportContents.Where(r => r.fitId == "29" && r.reportId == maxReportId && r.nodeId != null).Select(a => a.nodeId).Distinct().ToList() ;
这篇关于实体框架查询嵌套查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文