Из того, что я понимаю о, где пункт в LINQ, это комбинирует элементы от двух или больше наборов на основе всех возможных комбинаций каждого элемента и затем применяет критерии. Например:
public static void Main(string[] args)
{
var setA = new[] {3, 4, 5};
var setB = new[] {6, 7, 8};
var result = from a in setA
from b in setB
let sum = a + b
where sum == 10 // Where, criteria sum == 10.
select new {a, b, sum};
foreach (var element in result)
Console.WriteLine("({0},{1}) == {2}", element.a, element.b, element.sum);
Console.ReadLine();
}
Это приводит к следующим результатам перед, где критерии применяются.
3,6 = 9 4,6 = 10 5,6 = 11
3,7 = 10 4,7 = 11 5,7 = 12
3,8 = 11 4,8 = 12 5,8 = 13
Результаты, которые соответствуют критериям, 3,7 и 4,6. Это приводит к результатам:
(3,7) == 10
(4,6) == 10
Однако от то, что я помню в теории множеств начальной школы, является там способом обеспечить объединение двух наборов (псевдокод):
{3, 4, 5} объединение {6, 7, 8} = {3, 4, 5, 6, 7, 8}
Спасибо,
Scott
var Result = setA.Union(setB)
Для получения полного списка всех операторов посмотрите на Enumerable .
Where
не производит все возможные пары.... Где
фильтры.
SelectMany
производит все возможные пары:
var pairings = SetA
.SelectMany(a => SetB, (a, b) => new {a, b} );
или
var pairings =
from a in SetA
from b in SetB
select new {a, b};
В связи с отложенным выполнением, клеши запросов linq к объектам обрабатываются в другом порядке, чем вы можете заподозрить. Вспомните: запросы не обрабатываются до тех пор, пока они не будут перечислены (например, - используются в фораховом цикле или при вызове метода .ToList()). Рассмотрим исходный запрос с парой лишних вызовов методов:
var result = (
from a in setA
from b in setB
let sum = a + b
where sum == 10
select new {a, b, sum}
).Take(2).ToList();
Реальный порядок обработки продолжается таким образом
* ToList calls its inner enumerable
* Take(2) calls its inner enumerable
* Select calls its inner enumerable
* Where calls its inner enumerable
* from (setb) calls its inner enumerable
* from (seta) returns 3 from seta
* from (setb) returns a pairing of 3 and 6
* Where filters out the pairing and calls its inner enumerable for another one
* from (setb) returns a pairing of 3 and 7
* Where checks the pairing and returns it
* Select uses the pairing to create the anonymous instance.
*Take(2) returns the anonymous instance and remembers that it has returned once.
*ToList adds the element to a List
*ToList calls its inner enumerable
* Take(2) calls its inner enumerable
* Select calls its inner enumerable
* Where calls its inner enumerable
* from (setb) returns a pairing of 3 and 8
* Where filters out the pairing and calls its inner enumerable for another one
* from (setb) calls its inner enumerable
* from (seta) returns 4 from seta
* from (setb) returns a pairing of 4 and 6
* Where checks the pairing and returns it
* Select uses the pairing to create the anonymous instance.
*Take(2) returns the anonymous instance and remembers that it has returned twice.
*ToList adds the instance to a List.
*ToList askes Take(2) if there's any more, and Take(2) say no.
*ToList returns the list.
Союз - это только один из 101 примеров, перечисленных здесь: