问题描述
嘿,所以我遇到的情况是我从数据库中取回客户端,并通过包含所有案例研究包括
Hey so I've got the situation where I'm pulling a client back from the database and including all the case studies with it by way of an include
return (from c in db.Clients.Include("CaseStudies")
where c.Id == clientId
select c).First();
但我现在要做的是和包含的casestudies的where子句,以便它只返回案例研究删除=假
but what I want to do now is and a where clause on the included casestudies so that it only returns the case studies where deleted = false
有点像这样
return (from c in db.Clients.Include("CaseStudies")
where c.Id == clientId
&& c.CaseStudy.Deleted == false
select c).First();
但这不起作用:(任何想法
But this doesn't work :( any ideas
推荐答案
EF v1.0中不支持开箱即用的条件包含。但Alex James有一些hacky解决方法,因为这里解释得很好:
Conditional includes are not supported out-of-the-box in EF v1.0. But Alex James has a bit hacky workaround for that explained well here: 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);
此外,我没有成功地使这种解决方法适用于多对多关系。
Also, I haven't succeeded to make this workaround work with many-to-many relationships.
这篇关于Linq包含where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!