VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/linq-set-operator-union/

⇱ LINQ | Set Operator | Union - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

LINQ | Set Operator | Union

Last Updated : 22 May, 2019
In LINQ, Set operators are those operators in query expression which return a result set which is based on the existence or non-existence of the equivalent elements within the same or different collections or sequences or sets. The standard query operator contains the following set operators:
  1. Union
  2. Intersect
  3. Except
  4. Distinct

Union

The Union operator returns the set union. Or in other words, we can say that it returns the set which contains the unique elements that appear in the given input collections, or sequence. 👁 Image
  • It does not support query syntax in C# and VB.Net languages. But you can use Union method on query variable or you can wrap your query in brackets and then the call Union method.
  • It support method syntax in both C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • It is implemented by using deferred execution.
  • When you are working with the collections of complex types you must use IEqualityComparer interface, otherwise, the Union method will give you incorrect result.
Example 1:
Output:
Sequence 1 is: 
m
q
o
s
y
a
Sequence 2 is: 
p
t
r
s
w
z
Unique Sequence: 
m
q
o
s
y
a
p
t
r
w
z
Example 2:
Output:
Common Languages: 
C#
C
Java
Python
Comment
Article Tags:
Article Tags:

Explore