Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to create Dynamic Linq Queries

Posted on 2011-09-12
3
Medium Priority
?
405 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 23

Accepted Solution

by:
wdosanjos earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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