Dynamic where

4 posts, 0 answers
  1. Neil Mercer
    Neil Mercer avatar
    18 posts
    Member since:
    Sep 2009

    Posted 09 Apr 2010 Link to this post

    Hi,

    I'm new to both OA and Linq so don't know if the following is possible, but would be great to find a way.

    I have a query form with 12 fields the user can enter filter values for.  When they submit the form the filter values are passed to another page as part of the querystring, e.g:

    search.aspx?project=abc123&office=xyz 

    I would like to build a Linq query based on these filter values, however I don't know which fields the user is going to use for their filter.

    Currently I am using OA and Linq to query my datasource:

    Using context as JobTrackerEntityDiagrams = New JobTrackerEntityDiagrams  
       Dim proj = From c In Context.Projects _  
                  Select c  
     
    End Using 
    I know I can add Where clauses to the query like so (assuming the field being queried is 'Project' and I've put the querystring value into a variable called a_project :
    proj = proj.Where(Function(p) p.Project.Contains(a_project)) 
    But that only works when I know the user is filtering on the Project field.  I could build a huge number of Ifs and adjust the query in each one, but this seems very in-elegant.

    Any suggestions?

    Thanks,
    Neil.



  2. Neil Mercer
    Neil Mercer avatar
    18 posts
    Member since:
    Sep 2009

    Posted 09 Apr 2010 Link to this post

    OK, so with a little reading I've discovered the Dynamic Linq Query code, including the example posted in the Code Library.  Just to be contrary I'm trying to get this to work in VB.  Has anyone managed?

    Following the examples, I'm able to build a string that is my Where clause, however get the following error when attempting to apply the filter:

    Overload resolution failed because no accessible 'Where' can be called with these arguments.
  3. DevCraft banner
  4. Delton Phillips
    Delton Phillips avatar
    8 posts
    Member since:
    Mar 2010

    Posted 11 Apr 2010 Link to this post

    I'm interested to hear the answer on this also. I wrote code like below, as you said, not elegant but get's the job done.

                IQueryable<Person> PersonQuery;  
     
                PersonQuery = from obj in ObjectScope.Extent<Person>()  
                               select obj;  
     
                if ( firstName != null )  
                    PersonQuery = PersonQuery.Where( Person => Person.FirstName == firstName );  
     
                if ( lastName != null )  
                    PersonQuery = PersonQuery.Where( Person => Person.LastName == lastName );  
     
     
                IList<Person> PersonList = PersonQuery.ToList(); 

    I did find something on StackOverflow http://stackoverflow.com/questions/736952/the-best-way-to-build-dynamic-linq-query. This should be very helpful.

    Delton
    http://dellendinho.blogspot.com
  5. Serge
    Admin
    Serge avatar
    375 posts

    Posted 13 Apr 2010 Link to this post

    Hi Neil Mercer,

    One thing you can do here is use the Dynamic LINQ library. We have provided a code sample showing an example of this kind. Basically you can use this to build a string containing the where query and then pass it to Where method. It will automatically translate it to method calls.

    Another thing you could try is have a small predicate for each filter field and then depending on which fields the user used, combine the predicates to get the where clause for the filter query. You can see an example of combining predicates in this forum thread:

    http://www.telerik.com/community/forums/orm/linq-questions/dynamic-expression.aspx

    I hope this helps.

    Regards,
    Serge
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top