微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

查询无法翻译

如何解决查询无法翻译

我有以下课程:

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,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 重写查询以使其正常工作?是否可能,或者我是否必须检索内存中的文档然后运行查询?这并不理想,因为文档会随着时间的推移而增长...

提前致谢

解决方法

.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 语句可以正确翻译。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?