Linq의 Union Vs Concat
Union및에 대한 질문이 있습니다 Concat. 두 경우 모두 동일하게 행동한다고 생각합니다 List<T>.
var a1 = (new[] { 1, 2 }).Union(new[] { 1, 2 }); // O/P : 1 2
var a2 = (new[] { 1, 2 }).Concat(new[] { 1, 2 }); // O/P : 1 2 1 2
var a3 = (new[] { "1", "2" }).Union(new[] { "1", "2" }); // O/P : "1" "2"
var a4 = (new[] { "1", "2" }).Concat(new[] { "1", "2" }); // O/P : "1" "2" "1" "2"
위의 결과가 예상됩니다.
하지만 List<T>같은 결과를 얻고 있다면 .
class X
{
public int ID { get; set; }
}
class X1 : X
{
public int ID1 { get; set; }
}
class X2 : X
{
public int ID2 { get; set; }
}
var lstX1 = new List<X1> { new X1 { ID = 10, ID1 = 10 }, new X1 { ID = 10, ID1 = 10 } };
var lstX2 = new List<X2> { new X2 { ID = 10, ID2 = 10 }, new X2 { ID = 10, ID2 = 10 } };
var a5 = lstX1.Cast<X>().Union(lstX2.Cast<X>()); // O/P : a5.Count() = 4
var a6 = lstX1.Cast<X>().Concat(lstX2.Cast<X>()); // O/P : a6.Count() = 4
그러나 두 가지 모두 List<T>.
어떤 제안을 부탁드립니다.
Union은 Distinct값을 반환 합니다. 기본적으로 항목의 참조를 비교합니다. 항목에 다른 참조가 있으므로 모두 다른 것으로 간주됩니다. 기본 유형으로 캐스트하면 X참조가 변경되지 않습니다.
Equals및 GetHashCode(고유 항목을 선택하는 데 사용됨)을 재정의하면 항목이 참조로 비교되지 않습니다.
class X
{
public int ID { get; set; }
public override bool Equals(object obj)
{
X x = obj as X;
if (x == null)
return false;
return x.ID == ID;
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
}
But all your items have different value of ID. So all items still considered different. If you will provide several items with same ID then you will see difference between Union and Concat:
var lstX1 = new List<X1> { new X1 { ID = 1, ID1 = 10 },
new X1 { ID = 10, ID1 = 100 } };
var lstX2 = new List<X2> { new X2 { ID = 1, ID2 = 20 }, // ID changed here
new X2 { ID = 20, ID2 = 200 } };
var a5 = lstX1.Cast<X>().Union(lstX2.Cast<X>()); // 3 distinct items
var a6 = lstX1.Cast<X>().Concat(lstX2.Cast<X>()); // 4
Your initial sample works, because integers are value types and they are compared by value.
Concat literally returns the items from the first sequence followed by the items from the second sequence. If you use Concat on two 2-item sequences, you will always get a 4-item sequence.
Union is essentially Concat followed by Distinct.
In your first two cases, you end up with 2-item sequences because, between them, each pair of input squences has exactly two distinct items.
In your third case, you end up with a 4-item sequence because all four items in your two input sequences are distinct.
Union and Concat behave the same since Union can not detect duplicates without a custom IEqualityComparer<X>. It's just looking if both are the same reference.
public class XComparer: IEqualityComparer<X>
{
public bool Equals(X x1, X x2)
{
if (object.ReferenceEquals(x1, x2))
return true;
if (x1 == null || x2 == null)
return false;
return x1.ID.Equals(x2.ID);
}
public int GetHashCode(X x)
{
return x.ID.GetHashCode();
}
}
Now you can use it in the overload of Union:
var comparer = new XComparer();
a5 = lstX1.Cast<X>().Union(lstX2.Cast<X>(), new XComparer());
참고URL : https://stackoverflow.com/questions/13417556/union-vs-concat-in-linq
'Program Club' 카테고리의 다른 글
| 전체 파일 대신 한 줄을 git-checkout 할 수 있습니까? (0) | 2020.10.18 |
|---|---|
| 변수가 null인지 빈 문자열인지 또는 JavaScript에서 모두 공백인지 확인하는 방법은 무엇입니까? (0) | 2020.10.18 |
| 사용자 지정 유효성 검사 angularjs 지시문을 테스트하려면 (0) | 2020.10.18 |
| R에서 vlookup을 수행하고 Excel에서와 같이 채우는 방법은 무엇입니까? (0) | 2020.10.18 |
| 정적 라이브러리, 정적으로 연결된 동적 라이브러리 및 동적으로 연결된 동적 라이브러리의 .lib 파일에는 무엇이 있습니까? (0) | 2020.10.18 |