简体   繁体   English

C#:如何向列表中已存在的值添加新值<object> ?

[英]C#: How can I add a new value to a value which is already existing in list<object>?

I have two lists including string values and percentage .我有two lists包括字符串values and percentage I want to have duplicate string values once in my list and sum up their percentage.我想在我的列表中包含一次重复的字符串值并总结它们的百分比。 I don't know how can i access that specific place ( in reasonsName ) in the list ( reasonsPercent ).我不知道如何访问列表中的特定位置( in reasonsName )( reasonsPercent )。

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

How about using dictionary ?使用字典怎么样?

dictionary is Key-Value collection type字典是键值集合类型

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

In this case, you can use a model for the list.在这种情况下,您可以为列表使用模型。

First of all, we declare a class:首先,我们声明一个类:

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

}

next, declare a list of this class:接下来,声明这个类的列表:

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

and then, we need to find every item in the list.然后,我们需要找到列表中的每一项。 If it is found, we update it.如果找到,我们会更新它。 If not, we add the item to the list:如果没有,我们将项目添加到列表中:

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

if you want two lists and a percentage of float or double or 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();

or with a dictionary或者用字典

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "在 C# 控制台应用程序中向现有值添加新值" - Add new value to existing value in C# Console app 如何从对象中获取属性和值的名称并将此名称和值传递给 c# 中的列表 - How can i get the name of property and value from object and pass this name and value to a list in c# 如何将 CDATA 添加到已经存在的 ElementString? (C#) - How do I add a CDATA to an already existing ElementString? (C#) 我如何添加或减去mysql Db中已经存在的数据。(c#) - How can i Add or substract on data already existing in mysql Db.(c#) 无法在C#中为列表添加值 - Can't add value to List in C# 如何使用 C# 检查对象(我必须插入到列表中)是否已经在列表中? - How can I check if an object (that I have to insert in a list) is already in the list using C#? 如何使用DOM在C#中的现有XML文件中添加具有值的新子级 - How to add new child with value in existing XML file in C# using DOM C#如何将对象添加到具有对象列表的列表中 - C# how to add object to a list which has list of object 如何将新项目添加到现有列表 c#? - How to add new Items to an Existing List c#? 如何将列表框中的项的值添加到C#中的局部变量中? - How can I add the value of an item in a list box to a local variable in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM