Solved

How to create Dynamic Linq Queries

Posted on 2011-09-12
3
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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