Linq Union and intersect Example

Linq Union and intersect Example Linq Union Example.
  1. Linq Chapter 1 – Split string to list of strings.
  2. Linq Chapter 2 – Remove duplicates from list.
  3. Linq Chapter 3 – Linq Where Clause.
  4. Linq Chapter 4 – Linq Group by multiple clause.
  5. Linq Chapter 5 – Linq Inner Join query.
  6. Linq Chapter 6 – Linq Distinct list.
  7. Linq Chapter 7 – Linq OfType Method.

You want to perform union operation using Linq query. It's very simple using extension.

void Main()
{
   
   var arrFirstList = new string[] { "Asp","C#","VB.net","WCF" };
   var arrSecondList = new string[] { "WCF","VB.net","MVC","WPF", };
   
    var unionResults = arrFirstList.Union(arrSecondList);
    
    unionResults.Dump();
    
}

Output.

Result of Linq Union query shows all records from both list distinctly.

Asp
 
C#
 
VB.net
 
WCF
 
MVC
 
WPF
VB.net How to perform Union operation.
Private Sub Main()

	Dim arrFirstList = New String() {"Asp", "C#", "VB.net", "WCF"}
	Dim arrSecondList = New String() {"WCF", "VB.net", "MVC", "WPF"}

	Dim unionResults = arrFirstList.Union(arrSecondList)

	unionResults.Dump()

End Sub

Linq intersect example.

C# Linq Intersect example.
void Main()
{
    var arrFirstList = new string[] { "Asp","C#","VB.net","WCF" };
       var arrSecondList = new string[] { "WCF","VB.net","MVC","WPF", };
       
        var unionResults = arrFirstList.Intersect(arrSecondList);
        
        unionResults.Dump();
}
Output

Result of Intersect shows common data in two lists.

VB.net
 
WCF
VB.net Linq Intersect method.
Private Sub Main()
	Dim arrFirstList = New String() {"Asp", "C#", "VB.net", "WCF"}
	Dim arrSecondList = New String() {"WCF", "VB.net", "MVC", "WPF"}

	Dim unionResults = arrFirstList.Intersect(arrSecondList)

	unionResults.Dump()
End Sub

Tags:

Linq Group By Multiple ColumnsLinq Split String To ListLinq Inner JoinLearn Linq Step By StepLinq Distinct ListLinq Get Duplicate Count Of Words From StringHow to select multiple columns in Linq- C#.net VB.net codeC# ArrayList Search using Linq Extension OfType MethodC# MemoryStream ExampleLinq ToDictionary ExtensionLinq Array Order-By

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.