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

Related posts

Recent Viewed