【发布时间】:2021-04-13 00:46:36
【问题描述】:
我有以下课程:
class document
{
int Id;
List<Tag> Tags;
}
class Tag
{
int Id;
}
我想获取至少具有用户选择的标签之一的所有文档。
我编写了以下 linq 查询:
List<int> tagIds = tags.Select (x => x.Id).ToList ();
query.Where (doc => tagIds.Any (x => doc.Tags.Select (y => y.Id).Contains (x)));
如果我对文档列表执行它,它可以工作,但如果我使用 efcore 5 对 sqlite 数据库执行它,我会收到以下错误:
System.InvalidOperationException: 'The LINQ expression 'DbSet<DemaDocument>()
.Where(d => __tagIds_0
.Any(x => DbSet<Dictionary<string, object>>("DemaDocumentDemaTag")
.Where(d0 => EF.Property<Nullable<int>>(d, "Id") != null && object.Equals(
objA: (object)EF.Property<Nullable<int>>(d, "Id"),
objB: (object)EF.Property<Nullable<int>>(d0, "DocumentsId")))
.Join(
inner: DbSet<DemaTag>(),
outerKeySelector: d0 => EF.Property<Nullable<int>>(d0, "TagsId"),
innerKeySelector: d1 => EF.Property<Nullable<int>>(d1, "Id"),
resultSelector: (d0, d1) => new TransparentIdentifier<Dictionary<string, object>, DemaTag>(
Outer = d0,
Inner = d1
))
.Select(ti => ti.Inner.Id)
.Any(p => p == x)))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.'
如何使用 fluent LINQ 重写查询以使其正常工作?有可能,还是我必须检索内存中的文档然后运行查询?这并不理想,因为文档会随着时间的推移而增长......
提前致谢
【问题讨论】:
-
您是否尝试过反转查询:
query.Where(doc => doc.Tags.Any(x => tagIds.Contains(x.Id)))?这可能允许我们将Contains转换为IN语句,因为 tagsIds 列表对于查询是常量。 -
它就像一个魅力!您是否介意将其作为答案并解释我为什么它会起作用,以便将来我能够将此解决方案应用于其他问题?提前致谢!
标签: c# entity-framework linq ef-core-5.0