Explain Union, Intersect and Except?

Union – It returns the union of two collections. In union elements will be unique.

int[] numbers1 = {
	1, 2, 3, 4, 5
};
int[] numbers2 = {
	5, 6, 7, 8, 9, 10
};
IEnumerable < int > union = numbers1.Union(numbers2); //1,2,3,4,5,6,7,8,9,10

 Intersect – It returns the intersection of two collections.

int[] numbers1 = {
	1, 2, 3, 4, 5
};
int[] numbers2 = {
	5, 6, 7, 8, 9, 10
};
IEnumerable < int > intersection = numbers1.Intersect(numbers2); //5

 Except – It returns the difference between two collections. It is just opposite to intersect.

int[] numbers1 = {
	1, 2, 3, 4, 5
};
int[] numbers2 = {
	5, 6, 7, 8, 9, 10
};
IEnumerable < int > except = numbers1.Except(numbers2); //1,2,3,4

 

Tagged , . Bookmark the permalink.

Leave a Reply