[英]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.