假设我有一个简单的地址类,如下所示:
public class Address
{
public int AddressId { get; set; }
public List<int> NodeIds { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并填写了如下地址列表:
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}}
}
Run Code Online (Sandbox Code Playgroud)
并且我想在AddressIds上进行分组,因此结果列表将具有基本上卷起的NodeId,以防重复如下:
listOfAddressesWithoutDupes =
AddressId=1, NodeIds=List<int>{1,4,5},
AddressId=2, NodeIds=List<int>{2}},
AddressId=3, NodeIds=new List<int>{3}
Run Code Online (Sandbox Code Playgroud)
所以基本上我正在寻找一个能让我超越结果的groupby函数(或其他东西)
List<Address> listOfFilteredAddresses = listOfAddresses.GroupBy(x=>x.AddressId).Select(y=>new Address{AddressId=y.Key, NodeIds=?});
Run Code Online (Sandbox Code Playgroud)
提前致谢..
das*_*ght 11
你快到了:
List<Address> listOfFilteredAddresses =
listOfAddresses
.GroupBy(x=>x.AddressId)
.Select(y=>new Address{
AddressId=y.Key
, NodeIds=y.SelectMany(x=>x. NodeIds).ToList()
});
Run Code Online (Sandbox Code Playgroud)
这假设NodeIds在Address独特中; 如果不是,添加Distinct()后SelectMany.
| 归档时间: |
|
| 查看次数: |
23006 次 |
| 最近记录: |