Linq Group on Multiple Fields – VB.NET,Anonymous,Key

我被困了我需要帮助.我有一个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,.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
                                                    })
您可以使用 anonymous type并使用 Key keyword,以使相等的行为符合您的期望(C#不需要).

通过指定密钥前缀并删除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
}

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...