我有一個型別類 abc 的串列
public class abc
{
public int count;
public string country;
}
該串列可以具有類似的值
計數:1 - 國家:美國
計數:2 - 國家:美國
計數:3 - 國家:IND
計數:4 - 國家:英國
計數:5 - 國家:英國
現在我想把這個串列放入一個串列串列中,它應該根據國家/地區進行隔離。我的新串列應該是這樣的
計數:1 - 國家:美國
計數:2 - 國家:美國計數:3 - 國家:IND
計數:4 - 國家:英國
計數:5 - 國家:英國
計數可以是任何整數,國家可以是任何字串。
有什么簡單的方法可以做到這一點嗎?
uj5u.com熱心網友回復:
您可以使用GroupBy并在之后將每個組選擇到一個單獨的串列中:
List<abc> mylist = new List<abc>()
{
new abc{count = 1, country = "US"},
new abc{count = 2, country = "US"},
new abc{count = 3, country = "IND"},
new abc{count = 4, country = "UK"},
new abc{count = 5, country = "UK"},
};
List<List<abc>> result = mylist.GroupBy(x => x.country).Select(y => y.ToList()).ToList();
這樣你會得到一個包含 3 個其他串列的串列
uj5u.com熱心網友回復:
像這樣實作它:
List<abc> list = new List<abc>()
{
new abc() {country = "US", count = 1},
new abc() {country = "US", count = 2},
new abc() {country = "IND", count = 3},
new abc() {country = "UK", count = 4},
new abc() {country = "UK", count = 5}
};
Dictionary<string,List<abc>> dictionary = new Dictionary<string, List<abc>>();
foreach (var item in list)
{
if(!dictionary.TryGetValue(item.country,out var l))
{
l = new List<abc>();
dictionary.Add(item.country,l);
}
l.Add(item);
}
List<List<abc>> result = dictionary.Values.ToList();
uj5u.com熱心網友回復:
你可以這樣做。
List<abc> ls = new List<abc>();
ls.Add(new abc() { count = 1, country = "US" });
ls.Add(new abc() { count = 2, country = "US" });
ls.Add(new abc() { count = 3, country = "IND" });
ls.Add(new abc() { count = 4, country = "UK" });
ls.Add(new abc() { count = 5, country = "UK" });
List<List<abc>> listOfList = new List<List<abc>>();
foreach (var group in ls.GroupBy(x => x.country))
{
List<abc> list = new List<abc>();
foreach (var item in group)
{
list.Add(new abc() { count = item.count, country = item.country });
}
listOfList.Add(list);
}
LINQ
List<List<abc>> listOfList = new List<List<abc>>();
foreach (var (group, list) in from item in ls.GroupBy(x => x.country)
let temp = new List<abc>()
select (item, temp))
{
foreach (var item2 in group)
{
list.Add(new abc() { count = item2.count, country = item2.country });
}
listOfList.Add(list);
}
uj5u.com熱心網友回復:
像許多已經回答的人一樣,您可以通過 contryString 將您的串列分組。但是我個人更愿意將它添加到字典中,這樣訪問會更容易理解。
List<abc> myList = new List<abc>()
{
new abc{count = 1, country = "US"},
new abc{count = 2, country = "US"},
new abc{count = 3, country = "IND"},
new abc{count = 4, country = "UK"},
new abc{count = 5, country = "UK"},
};
您可以如上所述將它們分組:
var grouppedLists = myList.GroupBy(x => x.country).Select(y => y.ToList()).ToList();
或者你可以用它制作字典:
var myDictionary = myList.Select(item => item.country).Distinct().ToDictionary(country => country, country => myList.Where(item => item.country == country).ToList());
現在有了字典,您可以通過鍵(國家/地區)訪問特定串列。例如:
myDictionary["US"]; //would give you all items with the country "US"
您可以選擇您想使用的任何東西。請注意,如果您使用字典,則需要處理可能的keyNotFoundException
uj5u.com熱心網友回復:
最簡單的方法是使用 Linq。
您可以使用.GroupBy()基于屬性值創建分組 - 在本例中為Country.
在此示例中,.Select()陳述句使用命名元組使資料更具可讀性。
using System.Collections.Generic;
using System.Linq;
var data = new List<Abc>()
{
new()
{
Count = 1,
Country = "UK"
},
new()
{
Count = 2,
Country = "UK"
},
new()
{
Count = 3,
Country = "US"
}
};
var groupedData = data
.GroupBy(x => x.Country)
.Select(x => (Country: x.Key, Data: x.ToList()))
.ToList();
消費和使用此串列串列的方法如下:
foreach (var (country, groupData) in groupedData)
{
var groupDataString = string.Join(" ", groupData.Select(x => x.Count));
Console.WriteLine($"{country}: {groupDataString}");
}
示例輸出如下所示:
UK: 1 2
US: 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340159.html
上一篇:如何使用Zip<TFirst,TSecond>(IEnumerable<TFirst>,IEnumerable<TSecond>)?
下一篇:如何在映射串列中使用逗號代替逗號
