【问题标题】:Linq to object - Select DistinctLinq to object - 选择不同
【发布时间】:2010-03-18 14:26:23
【问题描述】:

我不太明白为什么这个 Linq 语句没有像我预期的那样工作:

    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)。

谁能告诉我哪里出错了?

谢谢

【问题讨论】:

    标签: .net vb.net linq


    【解决方案1】:

    您的匿名类型集合将通过引用值进行比较,而不是通过childId 字段的值。每个匿名类型都有不同的对象引用,因此它们都是不同的。

    只选择ID,不要投影成匿名类型。

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多