【问题标题】:C#. How to create List with new values and different size than original ListC#。如何创建具有新值和与原始列表不同大小的列表
【发布时间】:2017-04-21 15:06:16
【问题描述】:

例如主要数据是

主题作者问题

运动泰勒“问题”

历史泰勒“问题”

运动泰勒“问题”

我需要计算每个主题有多少问题,并显示我需要创建另一个列表的答案,其中仅包含我计算的主题和数字。 所以它应该是这样的:

运动 2

历史 1

数一数我有多少问题

static void Method(List<Class> list)
    {
        int sport = 0;
        int history = 0;
        for (int i = 0; i < list.Count; i++)
        {
            if (list[i].Theme == "History")
            {
                history++;
            }
            else if (list[i].Theme == "Sport")
            {
                sport++;
            }}

所以知道我正在尝试弄清楚如何通过 List 显示它

【问题讨论】:

  • 请您解释一下“如何通过列表显示”是什么意思?
  • 我的意思是在我数完问题后,我需要整理一下列表。所以我不知道如何整理列表,如果我不使用列表数组

标签: c# list arraylist


【解决方案1】:

听起来您只想要主题和计数?您可以按主题对项目进行分组并计算每个组中的项目数:

var sportsAndCounts = list
    .GroupBy(x => x.Theme)
    .Select(x => $"{x.Key} {x.Count()}");

这将创建您正在描述的字符串,但您可以将分组投射到您需要的任何内容。

【讨论】:

    【解决方案2】:

    简单的方法是像这样使用 Linq 和 group by 方法

    var query = from item in list
        group item by item.Theme into group
        select new { Question = group.Key, Count = group.Count()};
    
    foreach(var item in query){
        Console.WriteLine("{0} {1}", item.Question, item.Count);
    }
    
        Dictionary<string, int> themes = new Dictionary<string, int>();
    

    但您也可以使用字典来存储每个主题及其数量:

    Dictionary<string, int> themes = new Dictionary<string, int>();
    
    for (int i = 0; i < list.Count; i++)
    {
        if(themes.ContainsKey(list[i].Theme))
            themes[this[i].Theme]++;
        else
            themes[this[i].Theme = 1;
    }
    
    foreach(var keyValuePair in themes.OrderByDescending(x => x.Value).ThenBy(x => x.Key)){
        Console.WriteLine("{0} {1}", keyValuePair.Key, keyValuePair.Value);
    }
    

    【讨论】:

    • 它适用于我的字典,谢谢。但是我怎么能理清我得到的结果。例如按计数降序排列,如果计数等于字母顺序
    • 因为如果我有 List 数组,我只需在类中添加 IComparable 然后 list.Sort();但在这里我有两个不同的值
    • 我编辑了答案,所以字典先按计数排序,然后按主题排序。
    【解决方案3】:

    您可以通过Dictionary 做到这一点!只需看以下代码:

    static void Theme(List<Class> list) {
         var dict = new Dictionary<string, int>();
         for (var current in list) {
              if (dict.ContainsKey(current.Theme) {
                  dict[current.Theme]++;
              } else {
                  dict[current.Theme] = 1;
              }
         }
    }
    

    之后,您可以在 dict 中进行计数。只需使用例如dict["History"]

    【讨论】:

      【解决方案4】:

      使用实体框架..你可以做到这一点

       var query = list.GroupBy(p => p.Theme)
                     .Select(g => new { Sport = g.Key, Count = g.Count() });
      

      在 var 查询中,你应该拥有你所假装的东西。

      ps。没有测试代码;

      【讨论】:

        【解决方案5】:
        List<Question> QuestionList = new List<Question>
        {
            new Question{ Theme = "Sport", Author = "Tyler", T = "Question", },
            new Question{ Theme = "History", Author = "Tyler", T = "Question", },
            new Question{ Theme = "Sport", Author = "Tyler", T = "Question", },
        };
        
        var HistoryCount = QuestionList.Count(x => x.Theme == "History");
        var SportCount = QuestionList.Count(x => x.Theme == "Sport");
        

        HistoryCount 将为 2, SportCount 将为 1。

        这将要求您参考 System.Linq。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-24
          • 2023-03-30
          • 2018-11-09
          • 1970-01-01
          • 2017-08-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多