简体   繁体   English

“无法翻译 linq 表达式 [...]”

[英]"the linq expression [...] could not be translated"

I am trying to use LINQ methods to filte a list of client by cheking whether or not the client is in a table of my BDD我正在尝试使用 LINQ 方法通过检查客户端是否在我的 BDD 表中来过滤客户端列表

using(var db = new CrmXefi())
            {
                var activeAccount = db.AccountBase.Where(account => account.StateCode == 0);
                
                List<Client> toImport = clients.Where(client => activeAccount
                .Where(account => account.IsAccountSageNumberForAgency(client.CODECLIENT, new Guid("2CA81DBC-8261-EB11-B061-00155D53D276")))
                .Any()).ToList();
            }

LINQ cannot translate that expression. LINQ 无法翻译该表达式。 I've checked the type of my elements to make sure that none of the ones inside the queries are IEnumerable.我检查了元素的类型,以确保查询中的元素都不是 IEnumerable。 Can't find the origin of the problem.找不到问题的根源。

This expression works : var test = activeAccount.Where(account => account.IsAccountSageNumberForAgency("", new Guid("2CA81DBC-8261-EB11-B061-00155D53D276")));此表达式有效: var test = activeAccount.Where(account => account.IsAccountSageNumberForAgency("", new Guid("2CA81DBC-8261-EB11-B061-00155D53D276")));

is it because I get client.CODECLIENT in a Where() that is inside an other Where() and client is the in the first predicate?是因为我在另一个 Where() 内部的 Where() 中获得了 client.CODECLIENT 并且客户端是第一个谓词中的吗?

"Linq" is confusing here. “Linq”在这里令人困惑。 On one side you have the "traditional" Linq, that works on IEnumerable and does all the processing in memory.一方面,您拥有“传统”Linq,它适用于IEnumerable并在内存中进行所有处理。 On the other side you have Frameworks like Entity Framework that uses a Linq-like interface to compose queries.另一方面,您有像 Entity Framework 这样的框架,它使用类似 Linq 的接口来编写查询。 Ef Core is actually based on "Linq2Sql". Ef Core其实是基于“Linq2Sql”的。 EF converts your Linq to SQL and sends that to the server (it works on the IQueryable type. But not everything can be converted to SQL. In your case you're calling a specific method of a class, which is specific to your C# code. The SQL server doesn't know anything about it. And EF cannot convert it an expression. EF 将您的 Linq 转换为 SQL 并将其发送到服务器(它适用于IQueryable类型。但并非所有内容都可以转换为 SQL。在您的情况下,您正在调用特定于 C# 代码的类的特定方法.SQL 服务器对此一无所知。EF 无法将其转换为表达式。

Unless you can make something that can run on the server, you will need to pull the data to the local memory.除非您可以制作可以在服务器上运行的东西,否则您需要将数据拉到本地内存中。 You can do that with AsEnumerable .你可以用AsEnumerable做到这一点。

Eg例如

List<Client> toImport = clients
    .AsEnumerable()
    .Where(client => activeAccount
        .Where(account => account
            .IsAccountSageNumberForAgency(
                client.CODECLIENT,
                new Guid("2CA81DBC-8261-EB11-B061-00155D53D276")))
        .Any())
    .ToList();

Note, this will pull all data from the server to the local memory, thus costs memory and performance注意,这会将所有数据从服务器拉到本地内存,因此会消耗内存和性能

Your query cannot be executed on the database side, so you will have to do it client side, by loading all the objects in memory.您的查询无法在数据库端执行,因此您必须在客户端执行此操作,方法是将所有对象加载到内存中。 You can use ToList() to achieve this.您可以使用ToList()来实现这一点。 By example :举例:

using(var db = new CrmXefi())
{
    var activeAccount = db.AccountBase.Where(account => account.StateCode == 0);
    List<Client> toImport = clients.ToList().Where(client => activeAccount
    .Where(account => account.IsAccountSageNumberForAgency(client.CODECLIENT, new Guid("2CA81DBC-8261-EB11-B061-00155D53D276")))
    .Any()).ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM