Solved

How to create Dynamic Linq Queries

Posted on 2011-09-12
3
365 Views
Last Modified: 2012-05-12
I have a table with many fields. I can use LINQ to make selections with filters and sort expressions. My problem is that my filters and sorts cannot be coded at compilation time. They come from the user inputs at run time and they can be very general. For example, the user may decide to pick records that have (field1>(field2+field4) && field3==5) || (field4==(field2*field4) && field9.substring(7,5)==”hello”), etc. I think that you can code it with predicates. Have you done that? If so, can you show me some code fragments.
0
Comment
Question by:JoseHidalgo
3 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36526836
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 36538538
If you are using LINQ to Entities, you can leverage the ObjectSet<T>.Where method to dynamically build a query string with your criteria.  Here is an example:

using (var context = new YourEntities())
{
	var prms = new List<ObjectParameter>();
	
	prms.Add(new ObjectParameter("@field3", 5));
	prms.Add(new ObjectParameter("@field9", 9));
	
	var query = context.YourObjectSet.Where(
		"(it.field1>(it.field2+it.field4) && it.field3==@field3) || (it.field4==(it.field2*it.field4) && it.field9.substring(7,5)==@field9)",
		prms.ToArray()
		);
}          

Open in new window


ObjectQuery<T>.Where Method
http://msdn.microsoft.com/en-us/library/bb338811.aspx

I hope this helps.
0
 

Author Closing Comment

by:JoseHidalgo
ID: 36557042
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now