1

I'm asking if is possible in entity Framework to do something like this :

Dim criteria As ' Some Type ?? '

If option1 then criteria= ".vl1=5"
If option2 then criteria = criteria and ".vl2>3"
If option3 then criteria = criteria or "vl2=0"
If option4 then criteria = criteria and "name='name1'"

Dim query = context.Myobjects.Where(Criteria).ToList()

I want to know that is this possible ?

alex
  • 694
  • 3
  • 15
  • 35
  • See http://stackoverflow.com/questions/14621450/dynamic-where-clause-or-in-linq-to-entities and http://stackoverflow.com/questions/8540327/building-dynamic-where-clause-linq-to-sql – abatishchev Feb 06 '15 at 05:26
  • Also http://www.albahari.com/nutshell/predicatebuilder.aspx – abatishchev Feb 06 '15 at 05:27
  • You need append the where clause dynamically. It's pretty easy. See the links above. Basically, this is a duplicate of many similar questions. See more at https://www.google.com/search?q=entity+framework+dynamic+where+clause+site%3Astackoverflow.com – abatishchev Feb 06 '15 at 05:32
  • However feel free to ask if it won't be clear still. – abatishchev Feb 06 '15 at 05:33
  • I don't want to append where clause step by step. I want to know if it's possible to create like in my first post ? Actually I found this link that is very close what I want : http://geekswithblogs.net/thanigai/archive/2009/04/29/dynamic-query-with-entity-framework.aspx , but the type "ObjectParamter" doesn't exist. and I don't know if this work with Database First model ? – alex Feb 06 '15 at 05:38
  • Are you sure you want your criteria be string literals and do not be strongly typed? Then see Dynamic LINQ for Entity Framework, such as http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library. Also https://github.com/kahanu/System.Linq.Dynamic – abatishchev Feb 06 '15 at 05:41
  • It doesn't matter what approach do you use. It does matter what type of context do you use: DataContext, ObjectContext of DbContext (the later is the most, and actually - only. preferable). – abatishchev Feb 06 '15 at 05:45
  • Hello ! As I wrote before , in this link : http://geekswithblogs.net/thanigai/archive/2009/04/29/dynamic-query-with-entity-framework.aspx is a sample that is almost 100% what I want. But I have problem because in this sample there's a where clause like this : qry = dEntity.TourChecklist.Where(dynStr, ob.ToArray) , and on my project this produce error because the Where clause doesn't accept this kind of arguments. What's the problem ? – alex Feb 06 '15 at 18:43

0 Answers0