【发布时间】:2018-10-10 21:07:51
【问题描述】:
我想创建一个 linq to sql 查询,该查询将返回一个对象列表,其中包含一个已过滤的子列表。
听起来很简单,但我不知道如何让它发挥作用
这里是返回我想要的 SQL 查询:
select * from Texts t inner join Translations tt on t.TranslationId = tt.Id
inner join Pages p on tt.Id = p.TranslationId and tt.NeutralText = p.TitleNeutralTextId
where t.LanguageId = 1
现在我必须用 linq 写这个。
到目前为止我所做的是:
var query = this.Queryable() // Page entity
.AsNoTracking()
.Include(x => x.TitleTranslation.Texts);
return (from m in query
from l in m.TitleTranslation.Texts
where m.TitleTranslation.Texts.Any(l => l.LanguageId == 1)
select m);
但它不起作用,因为我得到了所有语言的子列表,而不是只有 id #1 的语言。
感谢您的帮助,
大卫
【问题讨论】:
-
考虑我的SQL to LINQ recipe中的规则,尤其是规则6。
-
对于 EF 我认为你需要使用
.Query方法来filter loading related entities。
标签: c# linq entity-framework-core