Linq多个字段组-VB.NET,匿名,密钥 [英] Linq Group on Multiple Fields - VB.NET, Anonymous, Key

查看:60
本文介绍了Linq多个字段组-VB.NET,匿名,密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.我需要帮助.我有一个重复的患者地址数据的DTO对象.我只需要获取唯一的地址即可.

I am stumped. I need help. I have a DTO object with duplicates patient address data. I need to get only the unique addresses.

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

我没有运气尝试过以下方法:

I have tried the following with no luck:

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

推荐答案

您可以使用 Key关键字为了使相等行为表现出您所期望的方式(C#不需要).

You can use an anonymous type and make use of the Key keyword in order for equality to behave the way you expect (not required for C#).

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

Change your grouping by specifying the Key prefix and remove the PatientAddress usage :

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
}

这篇关于Linq多个字段组-VB.NET,匿名,密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆