2010-03-18 48 views
1

我不能完全弄清楚爲什麼這個LINQ的聲明是不工作,因爲我所期望的:LINQ到對象 - SELECT DISTINCT

Dim distinctSurchargesList = (From thisparent As Parent In ThisParentCollection _ 
            From thisChild As Child In thisparent.theseChildren _ 
            Select New With {.childId = thischild.Id}).Distinct() 

我會假設,這將創建匿名類型的一個新的集合,這將是明顯的。相反,它會創建一個集合,其大小爲「ThisParentCollection」,其中包含重複的「MyAnonymousType」(重複的id)。

任何人都可以告訴我哪裏會出錯嗎?不是由childId字段的值 -

感謝

回答

4

你收集匿名類型的會進行比較,以供參考值相等。每個匿名類型都有不同的對象引用,因此它們都是不同的。

只需選擇ID,並且不要將其投影到匿名類型中。