說我有一個簡單的地址類象下面這樣:集團通過密鑰,並使用LINQ
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上進行分組,因此結果列表將具有基本上在如下重複的情況下捲起的節點ID:
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=?});
在此先感謝..
感謝迅速回應做。這工作:) – santosh212