嘿所以我遇到了这样的情况,我从数据库中拉回一个客户,并通过包含的方式包含所有案例研究
return (from c in db.Clients.Include("CaseStudies")
where c.Id == clientId
select c).First();
但我现在想要做的是在包含的案例研究中添加一个 where 子句,以便它只返回已删除 = false 的案例研究
有点像这样
return (from c in db.Clients.Include("CaseStudies")
where c.Id == clientId
&& c.CaseStudy.Deleted == false
select c).First();
但这不起作用:(任何想法
最佳答案
EF v1.0 不支持开箱即用的条件包含。但是亚历克斯詹姆斯有一个有点hacky的解决方法,在这里解释得很好:http://blogs.msdn.com/alexj/archive/2009/10/13/tip-37-how-to-do-a-conditional-include.aspx
var dbquery =
from c in db.Clients
where c.Id == clientID
select new {
client = c,
caseStudies = from cs in c.CaseStudy
where cs.Deleted==false
select cs
};
return dbquery
.AsEnumerable()
.Select(c => c.client);
此外,我还没有成功地使这种解决方法适用于多对多关系。
关于Linq 包含 where 子句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1680863/