多个字段上的 Linq 组 - VB.NET,匿名,密钥

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9997001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:02:20  来源:igfitidea点击:

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

vb.netlinqlinq-to-objects

提问by wavedrop

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

我难住了。我需要帮助。我有一个 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()

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     

and also

并且

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

回答by Ahmad Mageed

You can use an anonymous typeand make use of the Keykeywordin order for equality to behave the way you expect (not required for C#).

您可以使用匿名类型并利用Key关键字来使等式按照您期望的方式运行(C# 不需要)。

Change your grouping by specifying the Keyprefix and remove the PatientAddressusage :

通过指定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
}

回答by wavedrop

I have found the following code to work which essentially accomplishes the grouping that I desire and populating the new object as type PatientAddress therefore eliminating the use of anonymous objects and the keyword Key.

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

Maybe someone can explain it, at the moment I can not. Have a nice day.

也许有人可以解释一下,目前我不能。祝你今天过得愉快。

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

回答by Magnus

Its probably because PatientAddressdoes not override GetHashCodeand Equals. An alternative is to us an anonymous typefor the grouping. Try writing:

它可能是因为PatientAddress没有覆盖GetHashCodeEquals。对我们来说,另一种方法是使用匿名类型进行分组。试着写:

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