【问题标题】:C#: How can I add a new value to a value which is already existing in list<object>?C#:如何向 list<object> 中已存在的值添加新值?
【发布时间】:2021-11-13 07:31:37
【问题描述】:

我有two lists,包括字符串values and percentage。我想在我的列表中有一次重复的字符串值并总结它们的百分比。我不知道如何访问列表 (reasonsPercent) 中的特定位置 (in reasonsName)。

List<object> reasonsPercent = new List<object>();
List<string> reasonsName = new List<string>();

foreach (var item in lvShareRateListSorted)
{

    string reasonName = "";
    reasonName = item.EventReasonTitle;

    if (reasonsName.Contains(reasonName))
    {
        // here i want to add item.TotalPercent to a TotalPercent of reasonName which exists in reasonsName
    }
    else
    {
        reasonsName.Add(reasonName);
        reasonsPercent.Add(item.TotalPercent);
    }
}

【问题讨论】:

    标签: c# asp.net list


    【解决方案1】:

    在这种情况下,您可以为列表使用模型。

    首先,我们声明一个类:

    public class Item
    {
        public string ReasonTitle { get; set; }
        public float TotalPercent { get; set; }
    
    }
    

    接下来,声明这个类的列表:

    private static List<Item> items { get; set; } = new List<Item>();
    

    然后,我们需要找到列表中的每一项。如果找到,我们会更新它。如果没有,我们将该项添加到列表中:

    public void AddOrEdit(Item  item)
    {
        string reasonName = item.ReasonTitle;
    
        var searchResult = items.Find(x => x.ReasonTitle == reasonName);
        if (searchResult != null) // It is in the list
        {
            searchResult.TotalPercent += item.TotalPercent;
        }
        else
        {
            items.Add(item);
        }
    }
    

    【讨论】:

      【解决方案2】:

      dictionary怎么样?

      字典是Key-Value集合类型

      // You can add Content like 
      dictionary.Add(key, value);
      // remove
      dictionary.Remove(key);
      // access
      dictionary[key]
      
      var nameAndPercentages = new Dictionary<string, List<int>>();
      
      foreach (var item in lvShareRateListSorted)
      {
          string reasonName = item.EventReasonTitle;
          
          // if the name is not present in dictionary add and initalize list for percentages
          if (!nameAndPercentages.ContainsKey(reasonName))
          {
              nameAndPercentages.Add(reasonName, new List<int>());
          }
          // add percentage value to list which initialized above
          nameAndPercentages[reasonName].Add(item.TotalPercent);
      }
      
      foreach (var nameAndPercentage in nameAndPercentages)
      {
          Console.WriteLine($"Name: {nameAndPercentage.Key}, Sum: {nameAndPercentage.Value.Sum()}");
          // Same with Console.WriteLine("Name: " + nameAndPercentage.Key + ", Sum: " + nameAndPercentage.Value.Sum());
      }
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案3】:

      如果你想要两个列表和一个百分比的 float 或 double 或 int

      var groupedList = lvShareRateListSorted
          .GroupBy(x => x.EventReasonTitle)
          .Select(x => new { EventReasonTitle = x.Key, TotalPercent= x.Sum(y => y.TotalPercent) });
      
      reasonsPercent = groupedList.Select(x => x.TotalPercent).ToList();
      reasonsName = groupedList.Select(x => x.EventReasonTitle).ToList();
      

      或用字典

      var groupedList = lvShareRateListSorted
          .GroupBy(x => x.EventReasonTitle)
          .Select(x => new { EventReasonTitle = x.Key, TotalPercent= x.Sum(y => y.TotalPercent) });    
      
      var dictionary = groupedList.ToDictionary(x => x.EventReasonTitle, y => y.TotalPercent);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-15
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多