我有一個像下面的字符串列表,我填寫集團目前是這樣的:合併在列表重複的字符串,然後合計起來
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
您是否嘗試過使用HashTable而不是List,或者可能調用.Distinct()? – rmjoia
[使用linq刪除列表中的重複項](http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq) – Adam
@rmjoia我嘗試使用Distinct(),它沒有工作...還沒有用HashTable嘗試過 – User987