Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use union all in LINQ?

How to use union all in LINQ TO SQL. I have use the following code for union, then how to use this for union all?

List<tbEmployee> lstTbEmployee = obj.tbEmployees.ToList(); List<tbEmployee2> lstTbEmployee2 = (from a in lstTbEmployee                                     select new tbEmployee2                                     {                                         eid = a.eid,                                         ename = a.ename,                                         age = a.age,                                         dept = a.dept,                                         doj = a.doj,                                         dor = a.dor                                      }).Union(obj.tbEmployee2s).ToList(); 
like image 523
Brillia Avatar asked Apr 28 '12 04:04

Brillia


People also ask

How to use Union in LINQ query?

LINQ Union operator is used for finding unique elements between two sequences (Collections). For example, suppose we have two collections A = { 1, 2, 3 }, and B = { 3, 4, 5 }. Union operator will find unique elements in both sequences. { 3 } element is available in both sequences.

How does Linq Union work?

LINQ Union is an extension method that requires two collections to combine the two collections and returns the distinct elements from both collections. LINQ Union supports only method syntax; it does not support the query syntax. The Union method presents in both the classes Queryable class and Enumerable class.

What is difference Union and Union all in SQL?

The only difference between Union and Union All is that Union extracts the rows that are being specified in the query while Union All extracts all the rows including the duplicates (repeated values) from both the queries.

How to use Union operator in LINQ?

LINQ Union operator is used for finding unique elements between two sequences (Collections). For example, suppose we have two collections A = { 1, 2, 3 }, and B = { 3, 4, 5 }. Union operator will find unique elements in both sequences. { 3 } element is available in both sequences. So union operator only takes one element in the result.

How to find unique elements between two collections in LINQ?

LINQ Union operator is used for finding unique elements between two sequences (Collections). For example, suppose we have two collections A = { 1, 2, 3 }, and B = { 3, 4, 5 }. Union operator will find unique elements in both sequences. { 3 } element is available in both sequences.

Which classes can be used in LINQ Union method?

In LINQ Union method it only supports the method syntax; the query syntax will not be available in the union method. The Queryable and Enumerable classes will be acceptable in the union method.

What is the use of Union Union in Java?

Use of Union Union is an extension method to merge two collections. It requires at least two collections to perform the merge operation, but that merged collection holds only the distinct elements from both the collections. For better understanding, we will use an example.


1 Answers

Concat is the LINQ equivalent of UNION ALL in SQL.

I've set up a simple example in LINQPad to demonstrate how to use Union and Concat. If you don't have LINQPad, get it.

In order to be able to see different results for these set operations, the first and second sets of data must have at least some overlap. In the example below, both sets contain the word "not".

Open up LINQPad and set the Language drop-down to C# Statement(s). Paste the following into the query pane and run it:

string[] jedi = { "These", "are", "not" }; string[] mindtrick = { "not", "the", "droids..." };  // Union of jedi with mindtrick var union =   (from word in jedi select word).Union   (from word in mindtrick select word);  // Print each word in union union.Dump("Union"); // Result: (Note that "not" only appears once) // These are not the droids...  // Concat of jedi with mindtrick (equivalent of UNION ALL) var unionAll =   (from word in jedi select word).Concat   (from word in mindtrick select word);  // Print each word in unionAll unionAll.Dump("Concat"); // Result: (Note that "not" appears twice; once from each dataset) // These are not not the droids...  // Note that union is the equivalent of .Concat.Distinct var concatDistinct =   (from word in jedi select word).Concat   (from word in mindtrick select word).Distinct();  // Print each word in concatDistinct concatDistinct.Dump("Concat.Distinct"); // Result: (same as Union; "not" only appears once) // These are not the droids... 

The result in LinqPad looks like this:

enter image description here

like image 178
Jon Crowell Avatar answered Sep 19 '22 08:09

Jon Crowell