查询无法翻译

时间:2021-01-07 09:40:23

标签: c# entity-framework linq ef-core-5.0

我有以下课程:

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.'

如何使用流畅的 LINQ 重写查询以使其正常工作?是否可能,或者我是否必须检索内存中的文档然后运行查询?这并不理想,因为文档会随着时间的推移而增长...

提前致谢

1 个答案:

答案 0 :(得分:1)

.Contains 应翻译为 SQL IN 语句,即 x IN 1, 2, 3。这要求列表保持不变。在您的示例中,doc.Tags.Select (y => y.Id) 对于每个文档都是唯一的,因此无法将其转换为常量列表。

您正在做的是或多或少地检查两个列表是否相交,因此我们应该能够颠倒两个列表的顺序:

query.Where(doc => doc.Tags.Any(x => tagIds.Contains(x.Id)))

现在查询的 tagIds 是常量,并且 .Contains 语句可以正确翻译。