Using LINQ Queries, but making very flexible

I am curious how I can go about doing the following. I am creating an application that is similar to the custome data filter on excel. Here is an example of how the custom data filter would work:

If (Column A) "is greater than" 0

and

If (Column B) "is greater than" 0

A similar query in LINQ would look like:

var query = from s in CurrentDataContext.TableA
                   where (s.ColumnA > 0) && (s.ColumnB > 0)
                   select s;

The problem I am having is that the user is more or less setting its own rules, with an infanite number of conditions. Therefore, I cannot set a query for each possible query. I need to somehow use loops or just in general make it flexible enough to be able to do this.

Does anyone know how I can do that? If I need to elaborate, please let me know.

Thanks.
tiehazeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dstanley9Commented:
If you can build your conditions into strings you would call the .Where method rather than the Ling syntactic sugar:

string condition = "ColumnA > 0 && ColumnB > 0";

var query = CurrentDataContext.TableA
                   .Where(condition);

Here's some other examples:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
reknaCommented:
A better way is to use the predicate builder in the free library LinqKit because that way you don't loose the advandage of code completion and checking (http://www.albahari.com/nutshell/predicatebuilder.html):

IQueryable<Product> SearchProducts (params string[] keywords)
{
  var predicate = PredicateBuilder.False<Product>();

  foreach (string keyword in keywords)
  {
    string temp = keyword;
    predicate = predicate.Or (p => p.Description.Contains (temp));
  }
  return dataContext.Products.Where (predicate);
}

The above example uses OR to combine the conditions, but it you can easily construct an AND condition :

var predicate = PredicateBuilder.True<Product>();

if (a!=null)
  predicate = predicate.And( p => p.Description==a);

if (b!=null)
  predicate = predicate.And(p=>p.Category==b);

...
P.S. you can use there free tool LinqPad to test the queries
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.