Set Operators in LINQ C#
Set operators take two inputs , that can be array, list, collection etc and able to generate single output.
Set query operators:
- Concat
- Union
- Distinct
- Intersect
- Except
Let me discuss them one by one
- Concat
As it names says all the words about it, this operator is able to concat two collections, arrays , arraylist etc.
Example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExample { class Program { static void Main(string[] args) { string[] firstlist = { "item1", "item2", "item3", "item4" }; string[] secondlist = { "item5", "item6", "item7", "item8" }; //Lets use concat function var totalitems = firstlist.Concat(secondlist); //Read all items from totalitems Console.WriteLine("Total Items are"); Console.WriteLine("-----------------------------------------------------------"); foreach(string item in totalitems) { Console.WriteLine("Item is " + item); } Console.ReadLine(); } } }
Output
2. Union:
Union works in same way like concat operator but the difference is union removes all duplicate elements inoutput lists. Those elements which are already added from first input list and passed into output list, will not be added again either they exists in second list
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExample { class Program { static void Main(string[] args) { string[] firstlist = { "item1", "item2", "item3", "item4" }; string[] secondlist = { "item5", "item2", "item7", "item3" }; //Lets use Union function var totalitems = firstlist.Union(secondlist); //Read all items from totalitems Console.WriteLine("Total Items are"); Console.WriteLine("-----------------------------------------------------------"); foreach(string item in totalitems) { //here item 2 and item3 are exists in both list so they will not be duplicate in final list. Console.WriteLine("Item is " + item); } Console.ReadLine(); } } }
Output
3. Distinct
- The distict operator finds all the unique items in a single list
- Distict operator can be chained with other operator also.
- Distinct operator can be applied only on single list.
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExample { class Program { static void Main(string[] args) { string[] firstlist = { "item1", "item2", "item3", "item4", "item2", "item3", "item10" }; //Lets use Union function var totalitems = firstlist.Distinct(); //Read all unique items Console.WriteLine("All Unique items are"); Console.WriteLine("-----------------------------------------------------------"); foreach(string item in totalitems) { Console.WriteLine("Item is " + item); } Console.ReadLine(); } } }
Output
5. Except
The Except query operator will return only those elements in the first sequence where those same elements do not exist in the second sequence.It means The Except operator shows all the items in one list minus the items in a second list.
Example:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExample { class Program { static void Main(string[] args) { string[] firstlist = { "item1", "item2", "item3", "item4", "item2", "item3", "item10" }; string[] secondlist = { "item5", "item2", "item7", "item3" }; //Lets use Except function var totalitems = firstlist.Except(secondlist); //Read all items from totalitems Console.WriteLine("Total Items are"); Console.WriteLine("-----------------------------------------------------------"); foreach(string item in totalitems) { //It will show the list of those items from first list which are not exists in second input list Console.WriteLine("Item is " + item); } Console.ReadLine(); } } }
Output
Intersect
This linq query operator is used to common items from two list.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LINQExample { class Program { static void Main(string[] args) { string[] firstlist = { "item1", "item2", "item3", "item4", "item2", "item3", "item10" }; string[] secondlist = { "item5", "item2", "item7", "item3" }; //Lets use intersect function var totalitems = firstlist.Intersect(secondlist); //Read all items from totalitems Console.WriteLine("Total Items are"); Console.WriteLine("-----------------------------------------------------------"); foreach(string item in totalitems) { //Common Items are Console.WriteLine("Item is " + item); } Console.ReadLine(); } } }