我有一个像下面的字符串列表,我填写集团目前是这样的:合并在列表重复的字符串,然后合计起来
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