我有一个类型为abc的列表
public class abc
{
public int count;
public string country;
}
列表的值可以是
Count: 1 - country: US
Count: 2 - country: US
Count: 3 -country: IND
Count: 4 - country: UK
Count: 5 - country: UK
现在我想把这个列表放到一个应该根据国家进行隔离的列表中。我的列表的新列表应该是这样的
Count: 1 - country: US
Count: 3 - country: IND
Count: 2 - country: US
Count: 4 - country: UK
Count: 5 - country: UK
计数可以是任意整数,国家可以是任意字符串。
有什么简单的方法吗?
您可以使用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个其他列表的列表
实现如下:
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();
你可以这样做。
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);
}
就像许多已经回答的那样,您可以根据countryString
对您的列表进行分组。然而,我个人更倾向于将其添加到字典中,这样访问将更容易理解。
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 groupedLists = 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
最简单的方法是使用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