C# 将Sql查询转换为Lambda表达式-筛选子对象并返回父对象
我发现一个帖子正是我的问题,但没有一个正确的答案 点击 “我希望筛选出”具有子对象的父对象满足条件。家长不应仅返回符合条件的孩子。” 遵循我要转换lambda的sql查询:C# 将Sql查询转换为Lambda表达式-筛选子对象并返回父对象,c#,sql-server,linq,lambda,C#,Sql Server,Linq,Lambda,我发现一个帖子正是我的问题,但没有一个正确的答案 点击 “我希望筛选出”具有子对象的父对象满足条件。家长不应仅返回符合条件的孩子。” 遵循我要转换lambda的sql查询: SELECT * FROM RO INNER JOIN ROE ON ROE.ROId = RO.Id WHERE RO.TreeNode LIKE '.66.%' AND ROE.EnvironmentId = 3 我正在使用实体框架。我有两个实体:RO和ROE,遵循实体结构: public class RO {
SELECT
*
FROM
RO
INNER JOIN
ROE
ON ROE.ROId = RO.Id
WHERE RO.TreeNode LIKE '.66.%' AND ROE.EnvironmentId = 3
我正在使用实体框架。我有两个实体:RO和ROE,遵循实体结构:
public class RO
{
int ROId {get; set}
string ROName {get; set}
string TreeNode {get; set}
EntityCollection<ROE> ROEs {get; set}
}
public class ROE
{
int ROEId {get; set}
int ROId {get; set}
int environmentId {get; set}
string ROName {get; set}
}
但是这个表达式不起作用,因为ROEs集合没有被过滤
我的问题得到了回答。但我的问题是“包含”,因为当我尝试恢复某些子集合时,它不起作用。例如:
var ros = context.ROS.Include("T").Include("T.TOEs").Include("ROEs.EL")
.Where(ro => ro.TreeNode.StartsWith('.66.'))
.Select(ro => new
{
ro,
// "T" Object is working, but the child collection "T.TOEs" isn't working
T = ro.T,
// "ROEs" Collection is working, but the child collection "ROEs.EL" isn't working
ROEs = ro.ROEs.Where(roe => roe.EnvironmentId == environmentId)
})
.AsEnumerable()
.Select(x => x.ro)
.ToList();
抱歉。现在,我只使用“编辑”链接。我想这就是你的意思。@AdaltonVelosodeLima你的问题得到了回答吗?我的问题得到了回答。但我的问题是“包含”,因为当我尝试恢复一些子集合时,它不起作用。
var ros = context.ROS.Include("T").Include("T.TOEs").Include("ROEs.EL")
.Where(ro => ro.TreeNode.StartsWith('.66.'))
.Select(ro => new
{
ro,
// "T" Object is working, but the child collection "T.TOEs" isn't working
T = ro.T,
// "ROEs" Collection is working, but the child collection "ROEs.EL" isn't working
ROEs = ro.ROEs.Where(roe => roe.EnvironmentId == environmentId)
})
.AsEnumerable()
.Select(x => x.ro)
.ToList();