We use following set operations in LINQ:-
Distinct remove redundancy and show the unique records.
Figure 1
int[] techaltum = { 1, 2, 3, 4, 3, 4, 5, 19, 89, 4, 8, 2 };
IEnumerable<int> res = techaltum.Distinct();
Response.Write("Result after Distinct <br/>");
foreach (int data in res)
{
Response.Write(data+"<br/>");
}
The output of this code as follows:-
Figure 2
Except operator returns those values from first data source which do not repeated in second data source.
Figure 3
int[] techaltum1 = { 1, 2, 3, 4, 3, 4, 5, 19 };
int[] techaltum2 = { 100, 200, 300, 2, 14, 90, 1, 3 };
IEnumerable<int> res = techaltum1.Except(techaltum2);
Response.Write("Result after Except <br/>");
foreach (int data in res)
{
Response.Write(data+"<br/>");
}
The output of this code is as follows:-
Figure 4
Note: - except will not return those value which not in first data source but in second data source.
DataContext dc = new DataContext("Data Source=.;Initial Catalog=TechAltum;Integrated Security=True");
//sum without condition
int sum_without_cond = dc.GetTable<Class1>().Sum(prod_sum => prod_sum.no_of_prod);
//sum with conditon
int sum_with_cond = dc.GetTable<Class1>().Where(cond => cond.prod_year == 2001).Sum(prod_sum => prod_sum.no_of_prod);
Response.Write("Sum without conditon=" + sum_without_cond + "<br/>");
Response.Write("Sum with conditon=" + sum_with_cond + "<br/>");
Intersect returns those values which are common in both data Source.
Figure 5
int[] techaltum1 = { 1, 2, 3, 4, 3, 4, 5, 19,100 };
int[] techaltum2 = { 100, 200, 300, 2, 14, 90, 1, 3 };
IEnumerable<int> res = techaltum1.Intersect(techaltum2);
Response.Write("Result after Intersect <br/>");
foreach (int data in res)
{
Response.Write(data+"<br/>");
}
Output of this code:-
Figure 6
It will show the sequence value of two data source.
First it will show values of first data source and then second data source but it shows repeated value only one time.
Figure 7
int[] techaltum1 = { 1, 3, 4 };
int[] techaltum2 = { 2, 200,3, 300,14 };
IEnumerable<int> res = techaltum1.Union(techaltum2);
Response.Write("Result after Union <br/>");
foreach (int data in res)
{
Response.Write(data+"<br/>");
}
Output of this code:-
Figure 5
For any query you can send mail at info@techaltum.com
Thanks