简体   繁体   English

实体框架查询嵌套查询

[英]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! 我知道此查询可以满足我的要求,但是我不确定如何将其转换为实体框架!

Here was my attempt. 这是我的尝试。

var prevSelectedNodes = db.reportContents.Where(
f => f.fitId == id).Select(
f => f.nodeId).Distinct().ToList();

I need to somehow put a .Select() in the where call. 我需要以某种方式将.Select()放在where调用中。 However that kind of thing dosen't seem possible 但是这种事情似乎不可能

Thank you in advance! 先感谢您!

As you can't make two LINQ nested lambda expression. 由于您无法制作两个LINQ嵌套的lambda表达式。 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() ; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM