int[] numbers1 = { 1, 1, 2, 3, 3, 4, 5, 5, 6, 7, 7, 8, 9, 9, 10} ;
int[] numbers2 = { 1, 3, 5, 7, 9} ;
IEnumerable
union = numbers1.Union(numbers2);
foreach (int num in union)
listBox1.Items.Add(num);
When the Union operator is applied in this example, the following results are returned:
1
2
3
4
5
6
7
8
9
10
Intersect
The intersect operator returns the intersection of two sequences??”that is, those values that are common
between two sequences or collections.
The following example uses two lists (or data sources) that contain integer values. Again, you can see
that there are numbers in the first list that also exist in the second list. The intersect operator is applied;
it joins the two lists and returns only those values that are common to both sequences.
int[] numbers1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ;
int[] numbers2 = { 2, 4, 6, 8, 10} ;
IEnumerable shared = numbers1.Intersect(numbers2);
70
Chapter 4: LINQ Standard Query Operators
foreach (int num in shared)
listBox1.Items.Add(num);
The output is as follows:
2
4
6
8
10
Except
The Except operator is the opposite of the intersect operator, in that it returns the difference between
two sequences??”in other words, it returns values that are unique (not duplicated) in all of the values of
the sequences (values that appear in the first sequence but do not appear in the second).
Pages:
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145