You can program 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 the condition, add additional restrictions.
if(condition) query = query.Where(i => i.PONumber == "ABC");
I'm not sure how to encode this with the query syntax, but id works with lambda. Also works with query syntax for the original query and lambda for the secondary filter.
You can also include an extension method (below), which I encoded a while ago to include a condition that contains statements. (Doesn't work with 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);
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; }
andleer Mar 10 '09 at 22:14 2009-03-10 22:14
source share