wav*_*rop 16 linq vb.net linq-to-objects
我很难过.我需要帮助.我有一个带有重复患者地址数据的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()
Run Code Online (Sandbox Code Playgroud)
我试过以下没有运气:
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
Run Code Online (Sandbox Code Playgroud)
并且
PatientAddressDto = PatientAddressDto.GroupBy(Function(p) New PatientAddress With {
.Address1 = p.Address1,
.Address2 = p.Address2,
.City = p.City,
.State = p.State,
.Zip = p.Zip
})
Run Code Online (Sandbox Code Playgroud)
Ahm*_*eed 23
您可以使用匿名类型并使用Key关键字,以使相等的行为符合您的预期(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
}
Run Code Online (Sandbox Code Playgroud)
我发现以下代码可以工作,它基本上完成了我想要的分组,并将新对象填充为 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()
Run Code Online (Sandbox Code Playgroud)