?
Solved

How to create Dynamic Linq Queries

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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