LinQ 交集

LinQ 交集

LinQ 交集

Enumerable

1.Enumerable.Except

void Main()
{
	List<string> oldList = new List<string>(new string[] { "1", "2", "3" });
	List<string> newList = new List<string>(new string[] { "2", "4", "6" });

    //從newList中,取出不在oldList中 
	var diffList1 = newList.Except(oldList);
	foreach(var item in diffList1){
		Console.WriteLine(item);  //4,6
	}
	
    從oldList中,取出不在newLsit中
	var diffList2 = oldList.Except(newList);
	foreach(var item in diffList2){
		Console.WriteLine(item);   // 1,3
	}
}

2.Enumerable.Intersect

List A : { 1 , 2 , 3 , 5 , 9 }

List B : { 4 , 3 , 9 }

var intersectedList = list1.Intersect(list2);

//result : 3,9

 

參考網址:dotblogsdotblogs2