entity framework中的LINQ to Entities不支持LINQ表达式节点类型“Invoke”

任何人都可以帮助我解决我的问题。 我使用下面给出的代码:

public IEnumerable Getdata(Expression<Func> predicate) { return AccountsContext.InvoiceHeaders.Include("Company").Include("Currency") .Include("BusinessPartnerRoleList").Include("DocumentType") .Where(predicate); } 

…..

在我的代码中我使用如下

 Expression<Func> predicate = PredicateBuilder.True(); predicate = predicate.And(o => o.CompanyId == oInvoiceHeader.CompanyId); List lstInvheader=Getdata(predicate).ToList(); 

通过这样做,我得到了例外。 [System.NotSupportedException] — {“LINQ to Entities中不支持LINQ表达式节点类型’Invoke’。”}

使用Joe Albahari在LINQKIT中提供的AsExpandable()方法可以解决这个问题。 他是PredicateBuilder创建者,我看到你正在使用它。

如果使用Entity Framework进行查询,请将最后一行更改为:

return objectContext.Products.AsExpandable().Where(predicate);

你可以在这里获取LINQKIT DLL或在这里通过NuGet包安装它。

它肯定会解决你的问题,因为它解决了我的问题。

Linq到EF查询被翻译成SQL。 该exception意味着运行时无法将代码转换为SQL查询,因为它是SQL不支持的。

您可以更改代码以省略SQL不支持的部分,或者您可以首先通过调用.AsEnumerable()从数据库中提取数据,然后您可以执行所有操作,因为它是Linq-to-Objects

 public IEnumerable Getdata(Expression> predicate) { return AccountsContext.InvoiceHeaders.Include("Company").Include("Currency") .Include("BusinessPartnerRoleList").Include("DocumentType") .AsEnumerable() .Where(predicate); } 

我有一个案例,你的案件中的“InvoiceHeaders”是IEnumerable。 添加.AsQueryable()解决了这个问题。 参考: http : //blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx

所以,最终的代码看起来像

 return AccountsContext.InvoiceHeaders // omitted includes .AsQueryable() .Where(predicate);