【问题标题】:Linq Group on Multiple Fields - VB.NET, Anonymous, Key多个领域的 Linq 组 - VB.NET,匿名,密钥
【发布时间】:2012-04-24 21:16:21
【问题描述】:

我被难住了。我需要帮助。我有一个带有重复患者地址数据的 DTO 对象。我只需要获取唯一的地址。

Dim PatientAddressDto = New List(Of PatientAddress)

{Populate PatientAddressDto with lots of duplicate data}

PatientAddressDto = (From d In PatientAddressDto
                    Group d By PatientAddressDtoGrouped = New PatientAddress With {
                                                              .Address1 = d.Address1,
                                                              .Address2 = d.Address2,
                                                              .City = d.City,
                                                              .State = d.State,
                                                              .Zip = d.Zip
                                                              }
                  Into Group
                  Select New PatientAddress With {
                                                  .Address1 = PatientAddressDtoGrouped.Address1,
                                                  .Address2 = PatientAddressDtoGrouped.Address2,
                                                  .City = PatientAddressDtoGrouped.City,
                                                  .State = PatientAddressDtoGrouped.State,
                                                  .Zip = PatientAddressDtoGrouped.Zip
                                                  }).ToList()

我尝试了以下方法,但没有成功:

PatientAddressDto = (From d In PatientAddressDto
                    Select New PatientAddress With {
                                                  .Address1 = d.Address1,
                                                  .Address2 = d.Address2,
                                                  .City = d.City,
                                                  .State = d.State,
                                                  .Zip = d.Zip
                                                    }).Distinct     

还有

PatientAddressDto = PatientAddressDto.GroupBy(Function(p) New PatientAddress With {
                                                  .Address1 = p.Address1,
                                                  .Address2 = p.Address2,
                                                  .City = p.City,
                                                  .State = p.State,
                                                  .Zip = p.Zip
                                                    })

【问题讨论】:

  • 你的评论是对的,我会删除我的答案,因为它没有帮助。
  • 您是否尝试过只选择要检查的字段,然后在结果上使用 .Distinct()?
  • 米奇,是的,我有。我在上面编辑了我的评论以包含一些替代方案。

标签: vb.net linq linq-to-objects


【解决方案1】:

您可以使用 anonymous type 并使用 Key keyword 以使相等以您期望的方式运行(C# 不需要)。

通过指定 Key 前缀更改您的分组并删除 PatientAddress 用法:

Group d By PatientAddressDtoGrouped = New With {
    Key .Address1 = d.Address1,
    Key .Address2 = d.Address2,
    Key .City = d.City,
    Key .State = d.State,
    Key .Zip = d.Zip
}

【讨论】:

  • 感谢您的提示。我收到一个错误,但指出正在初始化的名称或属性必须以'.'开头。
  • @wavedrop 已更新。您需要在分组时删除New 声明中的PatientAddress,以便使用匿名类型。
  • 它不是任何匿名类型。类型为“患者地址”
  • @wavedrop 是对的,但是为了分组的目的,您可以按照上面的建议更改代码以使用匿名类型并按预期获得不同的结果。您最后的Select New PatientAddress 语句将确保最终的结果将是类。否则,您将需要修改您的PatientAddress 类并实现IEqualityComparer。如果您打算进行大量类似的分组和不同的调用,这可能是值得的,但是通过分组我们可以使用这个快捷方式。
  • 有趣的是,c# 中的等效代码比声称是基本的语言更基本......
【解决方案2】:

我发现下面的代码可以工作,它基本上完成了我想要的分组并将新对象填充为 PatientAddress 类型,因此消除了匿名对象和关键字 Key 的使用。

也许有人可以解释它,目前我不能。祝你有美好的一天。

Dim PatientAddressDto = New List(Of PatientAddress)

{Populate PatientAddressDto with lots of duplicate data}

PatientAddressDto = (From d In PatientAddressDto
                    Group d By d.Address1,
                                d.Address2,
                                d.City,
                                d.State,
                                d.Zip Into g =
                    Group Let grp = New PatientAddress With {.Address1 = Address1,
                                                                .Address2 = Address2,
                                                                .City = City,
                                                                .State = State,
                                                                .Zip = Zip}
                    Select grp).ToList()

【讨论】:

    【解决方案3】:

    这可能是因为PatientAddress 没有覆盖GetHashCodeEquals。 另一种方法是给我们一个anonymous type 用于分组。尝试写作:

    Group d By PatientAddressDtoGrouped = New With { Key .Address1 = d.Address1, ....
    

    【讨论】:

    • 你有推荐吗?
    • 我已经更新了答案。尝试使用匿名类型而不是现有类。
    • 感谢您的工作并且非常有帮助。我将 Ahmads 标记为答案,因为我只能选择一个,它稍微有点帮助,我认为他首先得到了最终答案(在您编辑之前)。再次感谢。
    猜你喜欢
    • 2016-08-28
    • 2023-02-10
    • 1970-01-01
    • 2013-06-17
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    相关资源
    最近更新 更多