User987 User987 - 1 month ago 8
ASP.NET (C#) Question

Merging duplicate strings in List and summing them

I have a list of strings like following which I fill and group currently like this:

public static List<CustomDTO> mostCommonKeywords { get; set; }


And the list is sorted like following:

mostCommonKeywords = key.GroupBy(v2 => v2)
.Select(g => new CustomDTO { Key = g.Key, Count = g.Count() })
.OrderByDescending(e => e.Count).Distinct()
.ToList();


Where Key is List of strings like following:

var key = new List<string>();


Each string element inside the key list consists of 3 words which I need to merge into 1 in case they are equal (or group them into one, whichever term you prefer more).

The grouping method like above gives me these results:

Samsung Galaxy S7
Galaxy S7 edge
Galaxy S7 Edge
S7 edge SM
Samsung Galaxy S7
Samsung Galaxy S7


As you can see clearly there are duplicates here in this list of strings, and I need the results to look like this:

Samsung Galaxy S7
Galaxy S7 edge
S7 edge SM


So basically wherever a same string occurs, I need to merge it into one...

What am I doing wrong here??

Edit: And here is how the CustomDTO class looks like:

public class CustomDTO
{
public string Key { get; set; }
public int Count { get; set; }

public List<int> Sales = new List<int>();
}


Edit: The thing here is that I'm adding a sale number into each string which consists of 3 words to know which keyword how many sales....

This is how I've done it:

for (int i = 0; i < filtered.Count; i++)
{
foreach (var triad in GetAllWords(filtered[i]))
{
var sequence = triad[0] + " " + triad[1] + " " + triad[2];
key.Add(sequence + " " + lista[i].SaleNumber);
}
}


This is the part that makes the string "not unique":

+ lista[i].SaleNumber


Edit:

mostCommonKeywords list is a list of CustomDTO object which consists of:

public string Key { get; set; }
public int Count { get; set; }
public List<int> Sales = new List<int>();


And suppose that at the end of everything the list looks like this:

Key Sales
Samsung Galaxy S7 5
Galaxy S7 edge 4
Galaxy S7 Edge 4
S7 edge SM 3
Samsung Galaxy S7 6
Samsung Galaxy S7 7


How can I now find all these duplicates and sum them so that the list looks like following:

Samsung galaxy S7 18
Galaxy S7 edge 8
S7 edge SM 3

Answer

When you group the strings you can pass a IEqualityComparer<> to ignore case:

var keywords = key.GroupBy(v2 => v2, StringComparer.InvariantCultureIgnoreCase)
                  .Select(g => new CustomDTO { Key = g.Key, Count = g.Count() })
                  .OrderByDescending(e => e.Count).Distinct()
                  .ToList();

EDIT:

If items are something like { string Key, int Sale }, you can Sum() the Sale property like this:

var keywords = items.GroupBy(v2 => v2.Key, StringComparer.InvariantCultureIgnoreCase)
                  .Select(g => new CustomDTO
                  {
                      Key = g.Key,
                      Count = g.Count(),
                      Sales = g.Sum(k => k.Sale)
                  })
                  .OrderByDescending(e => e.Count).Distinct()
                  .ToList(); 

Note: CustomDTO.Sales must be int type, not List<int>.

Comments