This is a migrated thread and some comments may be shown as answers.

Dynamic where

3 Answers 119 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Neil Mercer
Top achievements
Rank 1
Neil Mercer asked on 09 Apr 2010, 07:51 AM
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.



3 Answers, 1 is accepted

Sort by
0
Neil Mercer
Top achievements
Rank 1
answered on 09 Apr 2010, 08:49 AM
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.
0
Delton Phillips
Top achievements
Rank 1
answered on 11 Apr 2010, 06:34 AM
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
0
Serge
Telerik team
answered on 13 Apr 2010, 03:31 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Neil Mercer
Top achievements
Rank 1
Answers by
Neil Mercer
Top achievements
Rank 1
Delton Phillips
Top achievements
Rank 1
Serge
Telerik team
Share this question
or