1.
取交集 (A和B都有)
List A : { 1 , 2 , 3 , 5 , 9 }
List B : { 4 , 3 ,
9 }
var intersectedList =
list1.Intersect(list2);
結果 : { 3 , 9 }
判斷A和B是否有交集
boolean isIntersected = list1.Intersect(list2).Count() > 0
2.
取差集 (A有,B沒有)
List A : { 1 , 2 , 3 , 5 , 9 }
List B : { 4 , 3 ,
9 }
var expectedList =
list1.Except(list2);
结果: { 1 , 2
, 5 }
判断A和B是否有差集
boolean isExpected = list1.Expect(list2).Count() > 0
3.
取联集 (包含A和B)
List A : { 1 , 2 , 3 , 5 , 9 }
List B : { 4 , 3 ,
9 }
01 |
public static class ListExtensions
|
03 |
public static List<T> Merge<T>( this List<T> source, List<T> target)
|
05 |
List<T> mergedList = new List<T>(source);
|
06 |
mergedList.AddRange(target.Except(source));
|
11 |
var mergedList = list1.Merge(list2); |
对List取交集、联集及差集