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

3

23

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."}

Streusel answered 5/1, 2012 at 11:23 Comment(2)
You mention an exception. I think you forgot to add it.Cammi
If I remember correctly, this is due to how the PredicateBuilder you probably took from internet works. Try with this version: github.com/jbevain/mono.linq.expressions/blob/master/…Jackfruit
R
49

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.

Rorqual answered 3/7, 2012 at 15:11 Comment(0)
R
1

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);
}
Rhythm answered 5/1, 2012 at 11:37 Comment(2)
But by doing this i am getting the following error message 'System.Collections.Generic.IEnumerable<ShipNet.Accounting.Entities.InvoiceHeader>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,bool>)' has some invalid argumentsStreusel
Doing a where after .AsEnumerable() means that ALL records will be sucked in. Use this advice with EXTREME CAUTION.Tetrabrach
S
-2

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);
Supporting answered 12/12, 2014 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.