Solved

How to create Dynamic Linq Queries

Posted on 2011-09-12
3
354 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

17 Experts available now in Live!

Get 1:1 Help Now