已經只花了一個多小時的調試我們的代碼中的錯誤,其最終結果是一些關於Enumerable.Except方法,我們不知道:爲什麼Enumerable.Except返回DISTINCT項目?
var ilist = new[] { 1, 1, 1, 1 };
var ilist2 = Enumerable.Empty<int>();
ilist.Except(ilist2); // returns { 1 } as opposed to { 1, 1, 1, 1 }
或者更一般地說:
var ilist3 = new[] { 1 };
var ilist4 = new[] { 1, 1, 2, 2, 3 };
ilist4.Except(ilist3); // returns { 2, 3 } as opposed to { 2, 2, 3 }
望着MSDN頁:
此方法返回 :第一,不會出現在第二的元素。它 也不會返回那些不在第一個出現的第二秒內的那些元素 。
我明白了,在這樣的情況下:
var ilist = new[] { 1, 1, 1, 1 };
var ilist2 = new[] { 1 };
ilist.Except(ilist2); // returns an empty array
你得到空數組,因爲「出現」第一個數組中的每個元素在第二,因此應予以刪除。
但是,爲什麼我們只能得到不顯示在第二個數組中的所有其他項的不同實例?這種行爲的基本原理是什麼?
在我看來,氣味像一個錯誤或意想不到的功能。 MSDN頁面沒有提到一個獨特的結果... – 2010-12-20 18:11:54
這不是一個錯誤。 「Except」方法旨在被翻譯成定義爲集合操作的SQL「EXCEPT」運算符。作爲設置操作,只返回不同的元素。 MSDN使用「set」術語暗示「獨特性」。 – Gabe 2010-12-20 18:35:07