Is there a method in Linq where you can use to build SQL strings like "...where (a=1) OR (a=2)"?
-
6I assume you know how to use `||` and want something dynamic, like `a=a.where(hour=> hour<20); if(weekend) a=a.where(hour=> hour>6);`. You may want to state that more clearly... – Kobi Jan 20 '10 at 13:26
-
An earlier question with a different solution: https://stackoverflow.com/q/930677/11683 – GSerg Sep 01 '21 at 11:07
6 Answers
You can certainly do it within a Where clause (extension method). If you need to build a complex query dynamically, though, you can use a PredicateBuilder.
var query = collection.Where( c => c.A == 1 || c.B == 2 );
Or using a PredicateBuilder
var predicate = PredicateBuilder.False<Foo>();
predicate = predicate.Or( f => f.A == 1 );
if (allowB)
{
predicate = predicate.Or( f => f.B == 1 );
}
var query = collection.Where( predicate );

- 524,688
- 99
- 697
- 795
-
This worked great as I needed to build my Or depending upon the values of incoming parameters -- Awesome! – Mark Jun 27 '12 at 20:38
-
Very cool. Its a shame this isnt included as a function inside of .NET as standard. – maxp Aug 19 '13 at 11:19
-
1Very nice implementation, though it might not have been noted this only works for C# 5+. – Thomas.Donnelly Dec 29 '15 at 18:45
-
1I believe you have to `.Compile` the predicate before it can be passed to the `.Where`? – test Jun 02 '21 at 23:07
You can use the standard .NET boolean operators in your single where clause:
MyDataSource.Where(data => data.a == 'a' || data.a == 'b')

- 11,558
- 4
- 45
- 67
You use the all the same operators as in normal C# ===> || for "or" && for "and" etc.
var something = from s in mycollection
where s.something == 32 ||
s.somethingelse == 45
select s

- 28,542
- 5
- 55
- 68
in your .Where()
call use the standard Boolean 'Or' operator, ||
.
var query = items.Where(item => (item == 1 || item == 2));
All the Where call does is a Boolean comparison on anything you want, so you can fill it with as much conditional logic as you wish.

- 19,423
- 9
- 68
- 97
If you don' t know parameter count, you can use this:
Sample Data
var parameters= new List<string>{"a","d"};
var sampledata = new Dictionary<string,string>();
sampledata["a"] = "A";
sampledata["b"] = "B";
sampledata["c"] = "C";
sampledata["d"] = "D";
Code
var query = sampledata.AsQueryable();
var firstItemKey = sampledata.FirstOrDefault().Key;
var queryresult= sampledata.Where(x => x.Key == firstItemKey).AsQueryable();
foreach (var parameter in parameters.Skip(1))
{
queryresult=queryresult.Concat(query.Where(x => x.Key == parameter));
}
var result = queryresult.ToList();

- 439
- 4
- 6
This is built into .net now, not sure if it previously wasn't. Given an existing Linq query you can add a where clause that takes an array of strings (SearchStrings), and check if any of them match whatever object in the collection you're search. Using ToLower() just makes sure that you avoid case sensitivity in SQL queries.
query.Where(i => SearchStrings.Any(s => i.ToLower().Contains(s.ToLower()));
You can do the same thing for an 'and' predicate by matching all the words in the array to the collection's object.
query.Where(i => SearchStrings.All(s => i.ToLower().Contains(s.ToLower()));
In this example i correlates to each object in a collection, and s correlates to each string in the SearchStrings array.

- 1
-
1Note that 'Any' cannot be translated by an EF provider and will be evaluated locally resulting in a full table scan and in-memory filtering. – Wade Bee Apr 10 '19 at 12:35