programing

LINQ에서 Union All을 사용하는 방법은 무엇입니까?

nicescript 2021. 1. 14. 08:06
반응형

LINQ에서 Union All을 사용하는 방법은 무엇입니까?


LINQ TO SQL에서 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();

ConcatUNION ALLSQL 의 LINQ에 해당 합니다.

내가 사용하는 방법을 보여 LINQPad에 간단한 예제를 설정 한 UnionConcat. LINQPad 가 없으면 가져 오십시오.

이러한 집합 작업에 대해 다른 결과를 볼 수 있으려면 첫 번째 및 두 번째 데이터 집합이 적어도 일부 중복되어야합니다. 아래 예에서 두 세트 모두 "not"이라는 단어를 포함합니다.

LINQPad를 열고 Language 드롭 다운을 C # Statement (s)로 설정합니다. 다음을 쿼리 창에 붙여넣고 실행합니다.

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...

LinqPad의 결과는 다음과 같습니다.

여기에 이미지 설명 입력

참조 URL : https://stackoverflow.com/questions/10360518/how-to-use-union-all-in-linq

반응형