C# 合并列表中的重复字符串并对其求和
我有一个字符串列表,如下所示,我现在这样填充和分组:C# 合并列表中的重复字符串并对其求和,c#,asp.net,asp.net-mvc,linq,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Linq,Asp.net Mvc 5,我有一个字符串列表,如下所示,我现在这样填充和分组: public static List<CustomDTO> mostCommonKeywords { get; set; } Samsung Galaxy S7 Galaxy S7 edge S7 edge SM Key Sales Samsung Galaxy S7 5 Galaxy S7 edge 4 Galaxy S7 Edge 4 S7 edg
public static List<CustomDTO> mostCommonKeywords { get; set; }
Samsung Galaxy S7
Galaxy S7 edge
S7 edge SM
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
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();
其中Key是字符串列表,如下所示:
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>();
正如您可以清楚地看到的,在这个字符串列表中有重复项,我需要结果如下所示:
public static List<CustomDTO> mostCommonKeywords { get; set; }
Samsung Galaxy S7
Galaxy S7 edge
S7 edge SM
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
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类的外观:
public class CustomDTO
{
public string Key { get; set; }
public int Count { get; set; }
public List<int> Sales = new List<int>();
}
编辑:
mostCommonKeywords列表是CustomDTO对象的列表,包括:
public string Key { get; set; }
public int Count { get; set; }
public List<int> Sales = new List<int>();
我现在如何找到所有这些重复项并对其求和,以便列表如下所示:
Samsung galaxy S7 18
Galaxy S7 edge 8
S7 edge SM 3
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();
对字符串进行分组时,可以传递
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
属性:
public static List<CustomDTO> mostCommonKeywords { get; set; }
Samsung Galaxy S7
Galaxy S7 edge
S7 edge SM
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
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
您是否尝试使用哈希表而不是列表,或者调用.Distinct()?我尝试使用Distinct()的@rmjoia的可能重复项,它不起作用。。。还没有尝试过HashTable Yet您从哪里获得列表?数据库什么的?我试过了,应该可以用。提琴:阿图罗,快速提问,想象一下如果我留下重复的琴弦。。有没有一种方法可以让我在它们之间循环,找到所有重复的字符串,并一次性将它们的销售额相加??正如你所见,我有一个每个关键字的销售清单。。。我必须循环浏览整个列表,找到相同的列表并求和,然后将重复项(即,在新列表中只添加一个重复字符串,以确保其中没有重复项)添加到新的第三个列表中,该列表将包含所有销售总额…@User987:我不完全理解你的要求,请添加一个例子。我已经更新了我原来的问题,你能研究一下吗?有没有办法让销售列表成为销售列表?如果你想要销售值(而不是总和),你可以做Sales=g.Select(k=>k.Sale).ToList()
,但是在你的输出中,你显示的是一个单一的总和,所以Sales
必须是一个整数,不是列表。快速提问,想象一下如果我像那样留下重复的字符串。。有没有一种方法可以让我在它们之间循环,找到所有重复的字符串,并一次性将它们的销售额相加??正如你所见,我有一个每个关键字的销售清单。。。我必须在整个列表中循环,找到相同的列表并求和,然后将重复项(即,在新列表中只添加一个重复字符串,以确保其中没有重复项)添加到新的第三个列表中,该列表将包含所有销售总额。。。