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 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)) 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 WhereIf( this IQueryable source, bool condition, Expression> predicate) { if (condition) return source. Where(predicate); else return source; } Here is the same extension method for IEnumerables: public static IEnumerable WhereIf( this IEnumerable source, bool condition, Func predicate) { if (condition) return source. Where(predicate); else return source; }.

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 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)) 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 WhereIf( this IQueryable source, bool condition, Expression> predicate) { if (condition) return source.

Where(predicate); else return source; } Here is the same extension method for IEnumerables: public static IEnumerable WhereIf( this IEnumerable source, bool condition, Func predicate) { if (condition) return source. Where(predicate); else return source; }.

1 That really cleans up the code and communicates much better – jlembke Mar 10 '09 at 22:37 2 +10 Thanks so much for the answer. Just what I was looking. I really appreciate you taking the time to do this.

– RSolberg Mar 10 '09 at 23:08 No prob. I love stuff like this. – andleer Mar 10 '09 at 23:17 Where in WA do you live?

I am in Bellingham. – andleer Mar 10 '09 at 23:18 1 Ryan, still working at SJH / PeaceHealth. Loving my job.

Great people. Are you on FB? We really should carry on this conversation elsewhere.

Arobinson / gmail. – andleer Mar 10 '097 at 15:27.

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.

1 That's nice. I was trying to think of something similar earlier – jlembke Mar 10 '09 at 22:07.

Check here Roscoe, it may have been answered already.. stackoverflow.com/questions/11194/condit....

You have the ability to OR with ||. Check out this thread, as it might give you some nice pointers: stackoverflow.com/questions/403505/c-lin....

Case Sensitive Paths. How to do SQL Like % in Linq? More fluent C# / .

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions