linq to sql: conditional where clause
-
I have a problem getting linqtosql working right. I have three strings, where each one can be string.empty. In the query I have three where clauses, each one checks one cell for if the value (string) starts with the text given. The problem is, that if the string is empty, the result is always empty, because none of the values starts with string.empty. What I need is a statement which allows me to add conditional where clauses - e.g if the string is empty skip the where clause, else use it. For better understanding:
string s = textbox1.Text; string t = textbox2.Text; string u = textbox3.Text; var query = (select p from database where p.questionablestring1.StartsWith(s) //don' t do it if s == string.Empty where p.questionablestring2.StartsWith(t)//don' t do it if t == string.Empty where p.questionablestring3.StartsWith(u)//don' t do it if u == string.Empty select p).ToList();
Neither this workswhere p.questionablestring1.StartsWith(s != string.Empty ? s : null)
//Given Error: must not be null nor thisvar query = (select p from database (if (s != string.Empty) {p.questionablestring1.StartsWith(s) }) select p).ToList();
Thanks in advance for your help. -
I have a problem getting linqtosql working right. I have three strings, where each one can be string.empty. In the query I have three where clauses, each one checks one cell for if the value (string) starts with the text given. The problem is, that if the string is empty, the result is always empty, because none of the values starts with string.empty. What I need is a statement which allows me to add conditional where clauses - e.g if the string is empty skip the where clause, else use it. For better understanding:
string s = textbox1.Text; string t = textbox2.Text; string u = textbox3.Text; var query = (select p from database where p.questionablestring1.StartsWith(s) //don' t do it if s == string.Empty where p.questionablestring2.StartsWith(t)//don' t do it if t == string.Empty where p.questionablestring3.StartsWith(u)//don' t do it if u == string.Empty select p).ToList();
Neither this workswhere p.questionablestring1.StartsWith(s != string.Empty ? s : null)
//Given Error: must not be null nor thisvar query = (select p from database (if (s != string.Empty) {p.questionablestring1.StartsWith(s) }) select p).ToList();
Thanks in advance for your help.The following pops into mind (using boolean shortcutting):
string s = textbox1.Text;
string t = textbox2.Text;
string u = textbox3.Text;var query = (select p from database
where (!string.IsNullOrempty(s) && p.questionablestring1.StartsWith(s))
|| (!string.IsNullOrempty(t) && p.questionablestring2.StartsWith(t))
|| (!string.IsNullOrempty(u) && p.questionablestring3.StartsWith(u))
select p).ToList();I've just knocked this together quickly, but the principle seems as though it should be sound.
Deja View - the feeling that you've seen this post before.
-
I have a problem getting linqtosql working right. I have three strings, where each one can be string.empty. In the query I have three where clauses, each one checks one cell for if the value (string) starts with the text given. The problem is, that if the string is empty, the result is always empty, because none of the values starts with string.empty. What I need is a statement which allows me to add conditional where clauses - e.g if the string is empty skip the where clause, else use it. For better understanding:
string s = textbox1.Text; string t = textbox2.Text; string u = textbox3.Text; var query = (select p from database where p.questionablestring1.StartsWith(s) //don' t do it if s == string.Empty where p.questionablestring2.StartsWith(t)//don' t do it if t == string.Empty where p.questionablestring3.StartsWith(u)//don' t do it if u == string.Empty select p).ToList();
Neither this workswhere p.questionablestring1.StartsWith(s != string.Empty ? s : null)
//Given Error: must not be null nor thisvar query = (select p from database (if (s != string.Empty) {p.questionablestring1.StartsWith(s) }) select p).ToList();
Thanks in advance for your help.You can build a query step-by-step: var query = database; if (s != string.Empty) query = query.Where(p => p.questionablestring1.StartsWith(s)) if (t != string.Empty) query = query.Where(p => p.questionablestring2.StartsWith(t)) // you can also use LINQ syntax here: if (u != string.Empty) query = from p in query where p.questionablestring3.StartsWith(u) select p; var result = query.ToList();
-
The following pops into mind (using boolean shortcutting):
string s = textbox1.Text;
string t = textbox2.Text;
string u = textbox3.Text;var query = (select p from database
where (!string.IsNullOrempty(s) && p.questionablestring1.StartsWith(s))
|| (!string.IsNullOrempty(t) && p.questionablestring2.StartsWith(t))
|| (!string.IsNullOrempty(u) && p.questionablestring3.StartsWith(u))
select p).ToList();I've just knocked this together quickly, but the principle seems as though it should be sound.
Deja View - the feeling that you've seen this post before.
-
You can build a query step-by-step: var query = database; if (s != string.Empty) query = query.Where(p => p.questionablestring1.StartsWith(s)) if (t != string.Empty) query = query.Where(p => p.questionablestring2.StartsWith(t)) // you can also use LINQ syntax here: if (u != string.Empty) query = from p in query where p.questionablestring3.StartsWith(u) select p; var result = query.ToList();
-
ezazazel wrote:
Forum:LINQ and .NET 3.5 Subject:Re: linq to sql: conditional where clause Sender:ezazazel Date:Sunday, March 30, 2008 9:44 PM that's what I've been looking for. Thanx a lot and sorry for the late reply
Hey, no problem. I figured that it had worked because you hadn't been back asking for follow up.
Deja View - the feeling that you've seen this post before.
-
You can build a query step-by-step: var query = database; if (s != string.Empty) query = query.Where(p => p.questionablestring1.StartsWith(s)) if (t != string.Empty) query = query.Where(p => p.questionablestring2.StartsWith(t)) // you can also use LINQ syntax here: if (u != string.Empty) query = from p in query where p.questionablestring3.StartsWith(u) select p; var result = query.ToList();
hi Daniel, this one is AND condition. How to implement OR Condition. Thanks in Advance!
Have A Nice Day! Murali.M
Blog