I have two lists
including string 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
).
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);
}
}
3
Answers
How about using dictionary?
dictionary is Key-Value collection type
In this case, you can use a model for the list.
First of all, we declare a class:
next, declare a list of this class:
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:
if you want two lists and a percentage of float or double or int
or with a dictionary