假设我有一个简单的地址类,如下所示:
public class Address
{
public int AddressId { get; set; }
public List<int> NodeIds { get; set; }
}
并填充了如下地址列表:
List<Address> listOfAddresses = new List<Address>
{
new Address {AddressId=1, NodeIds=new List<int>{1}},
new Address {AddressId=2, NodeIds=new List<int>{2}},
new Address {AddressId=3, NodeIds=new List<int>{3}},
new Address {AddressId=1, NodeIds=new List<int>{4}},
new Address {AddressId=1, NodeIds=new List<int>{5}}
}
并且我想按 AddressIds 分组,因此结果列表将包含 NodeIds,如果出现重复,则基本上会汇总如下:
listOfAddressesWithoutDupes =
AddressId=1, NodeIds=List<int>{1,4,5},
AddressId=2, NodeIds=List<int>{2}},
AddressId=3, NodeIds=new List<int>{3}
所以基本上我正在看一个 groupby 函数(或其他东西),它会让我高于结果
List<Address> listOfFilteredAddresses = listOfAddresses.GroupBy(x=>x.AddressId).Select(y=>new Address{AddressId=y.Key, NodeIds=?});
提前致谢..