2016-11-01 70 views
1

我有一個像下面的字符串列表,我填寫集團目前是這樣的:合併在列表重複的字符串,然後合計起來

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

而且列表進行排序像以下:

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

其中密鑰是如下字符串列表:

var key = new List<string>(); 

密鑰列表中的每個字符串元素包含o f 3個單詞,如果它們相等,我需要將它們合併爲1(或將它們合併爲一個,無論您更喜歡哪個術語)。

像上面的分組方法給我的結果:

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

正如你可以清楚地看到這裏有重複的字符串的這個名單,我需要的結果是這樣的:

Samsung Galaxy S7 
Galaxy S7 edge 
S7 edge SM 

所以基本上任何一個相同的字符串發生,我需要合併成一個...

我在做什麼錯在這裏??

編輯:這裏是CustomDTO類的樣子:

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

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

編輯:這裏的事情是,我加入一個銷售數量爲每一個由3個字就知道字符串的關鍵字如何許多銷售....

這是多麼我已經做到了:

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

這是使字符串「不是唯一」的一部分:

+ lista[i].SaleNumber 

編輯:

mostCommonKeywords名單CustomDTO對象的列表,其中包括:

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

並假設在一切結束時,列表如下:

 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 

我現在如何找到所有這些重複項並將它們相加,以便列表如下所示:

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

您是否嘗試過使用HashTable而不是List,或者可能調用.Distinct()? – rmjoia

+0

[使用linq刪除列表中的重複項](http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq) – Adam

+0

@rmjoia我嘗試使用Distinct(),它沒有工作...還沒有用HashTable嘗試過 – User987

回答

2

當組字符串可以傳遞IEqualityComparer<>忽略大小寫:

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

編輯:

如果項目是像{ string Key, int Sale },你Sum()Sale屬性這樣:

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

注意:CustomDTO.Sales必須是int類型,而不是List<int>

+1

是的這個作品,謝謝! ) – User987

+0

Arturo,快速的問題,想象一下,如果我留下了那樣的重複字符串。是否有一種方法讓我循環遍歷它們,找到所有重複的字符串並一次性總結它們的銷售量?正如你所看到的,我有一個每個關鍵字的銷售清單......我將不得不循環遍歷整個清單,找到相同的清單並對它們進行彙總,然後添加重複項(即只添加1個重複字符串到新清單中以確保我沒有重複)進入新的第三個列表,其中將包含所有的銷售總額... – User987

+0

@ User987:我不明白你問什麼,請添加一個例子。 –

1

GroupBy需要第二個參數,您可以在其中指定EqualityComparer。

這應該工作。你不需要第二Distinct呼叫

var mostCommonKeywords = key.GroupBy(v2 => v2,StringComparer.OrdinalIgnoreCase) 
     .Select(g => new CustomDTO { Key = g.Key, Count = g.Count() }) 
     .OrderByDescending(e => e.Count) 
     .ToList(); 
+1

是它的工作原理,謝謝:) – User987

+0

快速的問題,想象如果我留下重複的字符串那樣..會有一種方法讓我循環通過他們,找到所有重複的字符串,並一次總結他們的銷售?正如你所看到的,我有一個每個關鍵字的銷售清單......我將不得不循環遍歷整個清單,找到相同的清單並對它們進行彙總,然後添加重複項(即只添加1個重複字符串到新清單中以確保我沒有重複)進入新的第三名單,其中將包含所有的銷售總和... – User987