How to use union all in LINQ?

前端 未结 1 900
一整个雨季
一整个雨季 2021-02-01 00:44

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 lstTbEmployee = obj.tbEm         


        
相关标签:
1条回答
  • 2021-02-01 00:57

    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:

    0 讨论(0)
提交回复
热议问题