"Lub" odpowiednik w Linq gdzie () wyrażenie lambda

Czy istnieje metoda w Linq, której możesz użyć do budowania łańcuchów SQL, takich jak "...where (a=1) OR (a=2)"?

Author: Liam, 2010-01-20

7 answers

Z pewnością możesz to zrobić w klauzuli Where (metoda rozszerzenia). Jeśli jednak chcesz zbudować złożone zapytanie dynamicznie, możesz użyć PredicateBuilder.

 var query = collection.Where( c => c.A == 1 || c.B == 2 );

Lub używając 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 );
 162
Author: tvanfosson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-20 13:26:31

Możesz użyć standardowych operatorów. NET boolean w klauzuli where:

MyDataSource.Where(data => data.a == 'a' || data.a == 'b')
 23
Author: Simon Steele,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-20 13:25:43

Używasz tych samych operatorów jak w normalnym C # = = = | / / for " or "& & for " and " etc.

var something = from s in mycollection
                where s.something == 32 || 
                      s.somethingelse == 45 
                select s
 19
Author: Muad'Dib,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-20 13:19:16
 4
Author: Randy Minder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-20 13:18:46

W wywołaniu .Where() Użyj standardowego Boolowskiego operatora 'Or', ||.

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 want.

 1
Author: Alastair Pitts,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-20 13:21:46

Jeśli nie znasz liczby parametrów, możesz użyć tego:

Przykładowe Dane

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";

Kod

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();
 1
Author: Bora Aydın,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-05 08:17:05

Jest to wbudowane w. Net teraz, Nie wiem, czy wcześniej nie było. biorąc pod uwagę istniejące zapytanie Linq, możesz dodać klauzulę where, która pobiera tablicę łańcuchów (SearchStrings) i sprawdzić, czy któryś z nich pasuje do dowolnego obiektu w kolekcji, którą przeszukujesz. Użycie metody ToLower() upewnia się, że unikasz rozróżniania wielkości liter w zapytaniach SQL.

query.Where(i => SearchStrings.Any(s => i.ToLower().Contains(s.ToLower()));

Możesz zrobić to samo dla predykatu 'and', dopasowując wszystkie słowa w tablicy do obiektu kolekcji.

query.Where(i => SearchStrings.All(s => i.ToLower().Contains(s.ToLower()));

W tym przykład i koreluje z każdym obiektem w kolekcji, A s koreluje z każdym ciągiem w tablicy SearchStrings.

 0
Author: JMacor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-09 18:33:58