【问题标题】:add all values to a list of list from a list将所有值从列表添加到列表列表
【发布时间】:2021-12-13 11:42:02
【问题描述】:

我有一个类型为 abc 的列表

public class abc
{
    public int count;
    public string country;
}

列表可以有类似的值

计数:1 - 国家:美国
计数:2 - 国家:美国
计数:3 - 国家:IND
数量:4 - 国家:英国
数量:5 - 国家:英国

现在我想将此列表放入应根据国家/地区隔离的列表列表中。 我的新列表应如下所示

计数:1 - 国家:美国
计数:2 - 国家:美国

计数:3 - 国家/地区:IND

计数:4 - 国家:英国
计数:5 - 国家:英国

计数可以是任意整数,国家可以是任意字符串。

有什么简单的方法吗?

【问题讨论】:

  • @TheGeneral 就是一个例子。我已经进一步更新了这个问题,使其不那么令人困惑。
  • 那么你想要多个列表吗?
  • 我想你对List 到底是什么有点困惑,你的Object abc 不是一个列表,它只有两个属性,countcountry ,它没有项目或集合,因此它不是List,如果您想要以下列表:CountryCount,您可以拥有一个List<string>,其中添加了所有Country 值,然后使用GroupBy 函数,然后您可以使用country 作为密钥,然后针对该密钥/国家/地区获取总Count
  • 我想要一个列表列表,所以是多个列表
  • @ConnorTJ 我在第一行中提到它是 abc 类型的列表

标签: c# list linq collections


【解决方案1】:

最简单的方法是使用 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

【讨论】:

    【解决方案2】:

    您可以使用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 个其他列表的列表

    【讨论】:

      【解决方案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();
      

      【讨论】:

      • dictionary.Values.ToList();这是一个有效的功能,你希望它被实现吗?
      • 其实是有效的。
      【解决方案4】:

      你可以这样做。

      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);
      }
      

      【讨论】:

        【解决方案5】:

        像许多已经回答的那样,您可以按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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-25
          • 2017-03-03
          • 1970-01-01
          • 2012-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多