创建一个包含公共元素和重复次数的列表
Create a List with the common elements and count of repetition
我有 4 个来自 4 个不同的 datagridview 的 var,我通过这样的 Linq 查询获得了一个值的选择:
var Sums = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells[7].Value != null)
.GroupBy(row => row.Cells[7].Value.ToString()) //inserire colonna gruppo
.Select(g => new { Gruppo = g.Key, Serie = g.Sum(row => Convert.ToInt32(row.Cells[1].Value)) });
所以每个变量看起来都类似于:
Gruppo | Serie
group1 | 3
group2 | 2
group3 | 7
我想要做的是获取一个列表,其中包含多个列表的交集和每个“Gruppo”元素的重复计数。
结果一定是这样的:
Gruppo | Repetition
group1 | 2 (if is present in two lists)
group2 | 3 (if is present in three lists)
group3 | 1 (if is present in one lists)
现在我有了这个代码的共同元素:
var pp = Sums.Intersect(Sums2);
但只找到具有相同“Gruppo”和相同“Serie”的元素,因此如果“gruppo”相等但 serie 不相等,则相交不起作用。
实现此目标的最佳方法是什么?
将它们放在一起并再次使用 GroupBy
两个属性:
var gruppoRepetitions = Sums.Concat(Sums2).Concat(Sums3)
.GroupBy(s => new{ s.Gruppo, s.Serie })
.Select(g => new{ Gruppo = g.Key.Gruppo, Repetition = g.Count() });
我有 4 个来自 4 个不同的 datagridview 的 var,我通过这样的 Linq 查询获得了一个值的选择:
var Sums = dataGridView1.Rows.Cast<DataGridViewRow>()
.Where(row => row.Cells[7].Value != null)
.GroupBy(row => row.Cells[7].Value.ToString()) //inserire colonna gruppo
.Select(g => new { Gruppo = g.Key, Serie = g.Sum(row => Convert.ToInt32(row.Cells[1].Value)) });
所以每个变量看起来都类似于:
Gruppo | Serie
group1 | 3
group2 | 2
group3 | 7
我想要做的是获取一个列表,其中包含多个列表的交集和每个“Gruppo”元素的重复计数。
结果一定是这样的:
Gruppo | Repetition
group1 | 2 (if is present in two lists)
group2 | 3 (if is present in three lists)
group3 | 1 (if is present in one lists)
现在我有了这个代码的共同元素:
var pp = Sums.Intersect(Sums2);
但只找到具有相同“Gruppo”和相同“Serie”的元素,因此如果“gruppo”相等但 serie 不相等,则相交不起作用。
实现此目标的最佳方法是什么?
将它们放在一起并再次使用 GroupBy
两个属性:
var gruppoRepetitions = Sums.Concat(Sums2).Concat(Sums3)
.GroupBy(s => new{ s.Gruppo, s.Serie })
.Select(g => new{ Gruppo = g.Key.Gruppo, Repetition = g.Count() });