1

I'm using PredicateBuilder ( From LinqKit).

This is my code :

Dim mylist as IQueryable(of MyObj1)
Dim pred1 = PredicateBuilder.True(Of Myobj1)()
Dim pred2 = PredicateBuilder.True(Of ch1)()
Dim pred3 = PredicateBuilder.True(Of ch2)()
pred1=pred1.And(Function(t) t.id=5)
pred2=pred2.And(Function(t1) t1.vl1>=7)
pred3=pred3.And(Function(t2) t2.quantity<=4)
mylist = (From t In context.Myobj1s.Where(pred1)Select New With { _
     .Parent = t, _
    .child1 = t.ch1s.AsQueryable.Where(pred2), _
.child2 = t.ch2s.Asqueryable.Where(pred3) _
 }).ToList

But I'm getting this error :

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

I try also to use AsExpandable instead of AsQueryable, but the problem is not resolved.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
alex
  • 694
  • 3
  • 15
  • 35

1 Answers1

1

There is an alternative to Linqkit's PredicateBuilder that doesn't use Expression.Invoke. This makes it far more suitable for predicates that are to be translated by a SQL provider.

If you use this predicate builder you can use your code without any changes.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Thank you , but please how can I use this method ? I mean is there a package on nugget that I should install , what should I include...etc ? , – alex Feb 07 '15 at 20:58
  • No, there isn't, sadly. You'll have to convert the source to VB code (and add it to your code base), or compile it as a C# class library and add it as a reference. – Gert Arnold Feb 07 '15 at 21:02
  • Does this also work with ordinary Linq collections, or only those using SQL providers? – Robert Harvey Feb 07 '15 at 21:14
  • @RobertHarvey I don't know what exactly you mean by "ordinary Linq collections", but there's no magic involved here. The builder just builds `Expression`s and you can use these anywhere where expressions apply. If you mean linq-to-objects, of course you know that the expressions should be compiled. Does this answer your question? – Gert Arnold Feb 07 '15 at 21:18
  • I think so, thanks. So this PredicateBuilder has a wider range; i.e. a larger set of more general cases under which it can be used. – Robert Harvey Feb 07 '15 at 21:19
  • @Gert Arnold : sorry , I don't know C# , but I put this code in new C# dll project , I generate the dll file , after I add a reference to this file on my project , I add the Imports UnivBuilder , but I get error on this line – alex Feb 07 '15 at 21:21
  • 1
    @Alex: You should probably spend some time trying to figure some of this out yourself. Gert has pointed you in a good direction, and as I pointed out in your previous question, we don't provide extended help or consultations here. We're just a question and answer site, and not particularly well suited to "learning programming, one question at a time" or "building applications, one question at a time." – Robert Harvey Feb 07 '15 at 21:23
  • @Robert : Yes , but if I can't include this on my project I can't say that is a good direction or doesn't work at all.Remember that I spent so much time in the previous question trying to use your suggestions and at the end I not resolved my problem. I'm asking for the library because I don't know c# – alex Feb 07 '15 at 21:28
  • Pasting code into a library, compiling that library and then referencing it in your project are all considered *essential skills.* You're expected to already know how to do these things. – Robert Harvey Feb 07 '15 at 21:29
  • Thank you ! As I wrote before , I done all these but doesn't work – alex Feb 07 '15 at 21:30
  • "It doesn't work" is not a problem statement. If you want troubleshooting help getting the new library to work in your project, write a new question that explains all the steps you did and showing the code you used, and specify exactly what the problem is, including any error messages that you are getting. – Robert Harvey Feb 07 '15 at 21:32
  • @alex The problem is, we can't see what's wrong. You probably made some mistake, same as I always stumble when I try to write VB. A new question would be OK. This gem deserves more exposure anyway. – Gert Arnold Feb 07 '15 at 21:33
  • 1
    Sorry friends. Was my mistake.I was targeting the C# library to .net 4.5 and I forget that my application was for Net 4.0 After make the changes everything works. – alex Feb 07 '15 at 22:33
  • @Gert : Your solutions is working only with Lazyloading=False , but if I set false Lazyloading , I need to use an include inside the predictions which seems to not working.Because ch1 has also a child collection ch11 that I need to load. Any solutions ? – alex Feb 07 '15 at 22:48
  • @alex, I refer to Robert's previous comment. You can't keep asking new questions in comments. There's such a thing as site etiquette. Plus it's not as effective a asking a new question with ample problem description. – Gert Arnold Feb 07 '15 at 23:01
  • @Gert : Sorry , I start a new question – alex Feb 07 '15 at 23:03