LINQ to SQL Where Clause Optional Criteria LINQ to SQL Where Clause Optional Criteria asp.net asp.net

LINQ to SQL Where Clause Optional Criteria


You can code your original query:

var query = from tags in db.TagsHeaders                where tags.CST.Equals(this.SelectedCust.CustCode.ToUpper())                 && Utility.GetDate(DateTime.Parse(this.txtOrderDateFrom.Text)) <= tags.ORDDTE                && Utility.GetDate(DateTime.Parse(this.txtOrderDateTo.Text)) >= tags.ORDDTE                select tags;

And then based on a condition, add additional where constraints.

if(condition)    query = query.Where(i => i.PONumber == "ABC"); 

I am not sure how to code this with the query syntax but id does work with a lambda. Also works with query syntax for the initial query and a lambda for the secondary filter.

You can also include an extension method (below) that I coded up a while back to include conditional where statements. (Doesn't work well with the query syntax):

        var query = db.TagsHeaders            .Where(tags => tags.CST.Equals(this.SelectedCust.CustCode.ToUpper()))            .Where(tags => Utility.GetDate(DateTime.Parse(this.txtOrderDateFrom.Text)) <= tags.ORDDTE)            .Where(tags => Utility.GetDate(DateTime.Parse(this.txtOrderDateTo.Text)) >= tags.ORDDTE)            .WhereIf(condition1, tags => tags.PONumber == "ABC")            .WhereIf(condition2, tags => tags.XYZ > 123);

The extension method:

public static IQueryable<TSource> WhereIf<TSource>(    this IQueryable<TSource> source, bool condition,    Expression<Func<TSource, bool>> predicate){    if (condition)        return source.Where(predicate);    else        return source;}

Here is the same extension method for IEnumerables:

public static IEnumerable<TSource> WhereIf<TSource>(    this IEnumerable<TSource> source, bool condition,    Func<TSource, bool> predicate){    if (condition)        return source.Where(predicate);    else        return source;}


Just need to use a conditional checking for the parameter's existence. For instance:

where (string.IsNullOrEmpty(ProductNumber) || ProductNumber == tags.productNumber)

That way if the product number isn't entered that expression will return true in all cases, but if it is entered it will only return true when matching.


You have the ability to OR with ||.

Check out this thread, as it might give you some nice pointers:C# LINQ equivalent of a somewhat complex SQL query