Home > Software engineering >  C#: How can I add a new value to a value which is already existing in list<object>?
C#: How can I add a new value to a value which is already existing in list<object>?

Time:11-13

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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