The LINQ expression node type 'Invoke' is not supported in LINQ to Entities in entity framework

廉价感情. 提交于 2019-11-27 04:40:13

问题


can anyone help me out in solving my issue. I am using the code given below:

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

.....

In my code I am using as below

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

By doing this I am getting the exception . [System.NotSupportedException] --- {"The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."}


回答1:


This problem can be solved using the AsExpandable() method present in LINQKIT by Joe Albahari. He's the same creator of PredicateBuilder that I see you're using.

If querying with Entity Framework, change the last line to this:

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

You can grab LINQKIT DLL here or install it through a NuGet package here.

It'll certainly solve your problem because it has solved mine.




回答2:


Linq to EF queries are translated into SQL. that exception means that the runtime can't translate your code into SQL query because it's something not supported in SQL.

you can either change your code to omit the parts that SQL doesn't support, or you can pull datas from the Database first by calling .AsEnumerable() like below, then you can do everything since it's Linq-to-Objects

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



回答3:


I had a case where "InvoiceHeaders" from your case was IEnumerable. Adding .AsQueryable() solved the issue. Reference: http://blogs.msdn.com/b/meek/archive/2008/05/02/linq-to-entities-combining-predicates.aspx

So, the code in the end looked like

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


来源:https://stackoverflow.com/questions/8741667/the-linq-expression-node-type-invoke-is-not-supported-in-linq-to-entities-in-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!