我很难过。我需要帮助。我有一个带有重复患者地址数据的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
})
答案 0 :(得分:23)
您可以使用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
}
答案 1 :(得分:4)
我找到了以下代码,它基本上完成了我想要的分组,并将新对象填充为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()
答案 2 :(得分:3)
可能是因为PatientAddress
未覆盖GetHashCode
和Equals
。
另一种选择是我们anonymous type进行分组。试着写:
Group d By PatientAddressDtoGrouped = New With { Key .Address1 = d.Address1, ....