Reputation: 46322
I have need to use Linq statement as following
var data = db.tbl1
.Where(w => clientIds == clientid && w.Source == source);
I have a dropdown where I am getting source from. The dropdown values are as such:
All
NewsPaper
Web
The thing is NewsPaper and Web are valid values for Source. All is not. All means that a record can be NewsPaper or Web.
If they choose All, how do I modify w.Source such that All means NewsPaper or Web. I can do 2 seperate queries as shown below but rather not:
if(source == "All")
{
var data = db.tbl1
.Where(w => clientIds == clientid);
}
else
{
var data = db.tbl1
.Where(w => clientIds == clientid && w.Source == source);
}
I like to do it in 1 query as the query I have is actually more complicated than what is shown above.
Thank you in advance
Upvotes: 3
Views: 115
Reputation: 38543
@mattytommo answer is acceptable, but I prefer to break them out a bit. IMO a little more readable.
var data = db.tbl1.Where(w => clientIds == clientid);
if (source != "All")
data = data.Where(w => w.Source == source);
Upvotes: 4
Reputation: 56449
Try this:
var data = db.tbl1
.Where(w => clientIds == clientid && (source == "All" || w.Source == source));
Upvotes: 5