C#获得系列中最受欢迎的两款产品组合
我收集了一些物品,如下所示:C#获得系列中最受欢迎的两款产品组合,c#,list,linq,C#,List,Linq,我收集了一些物品,如下所示: ID ELECT1 ELECT2 1 FDI AED 2 BPG AED 3 AED FDI 4 FDI AED 5 GPH AED 6 AED BPG 7 GPH
ID ELECT1 ELECT2
1 FDI AED
2 BPG AED
3 AED FDI
4 FDI AED
5 GPH AED
6 AED BPG
7 GPH FDI
我想对elect1和elect2进行分组,然后输出最受欢迎的两个
这些项目被选择了3次
FDI AED
AED FDI
FDI AED
第二个最流行的组合是
AED BPG
BPG AED
因此,输出将是
2 Most popular combinations are
FDI AED
AED BPG
我已经写了一些代码,但我不知道我怎么可能做到这一点
var groups = cStudent.GroupBy(x => new { x.elective1, x.elective2 });
foreach (var group in groups)
{
Console.WriteLine("{0} / {1}:", group.Key.elective1, group.Key.elective2);
foreach (var item in group)
{
Console.WriteLine(" {0} ({1})", item.elective1, item.elective2);
}
}
这就是我的代码所得到的,它已经完成了一半,只是不知道如何完成。
这应该适合您:
cStudent
.Select(x => new[] { x.elective1, x.elective2 }.OrderBy(y => y).ToArray())
.GroupBy(x => Tuple.Create(x[0], x[1]), (k, g) => new { Elective = k, Count = g.Count() })
.OrderByDescending(x => x.Count)
.Select(x => new { elective1 = x.Elective.Item1, elective2 = x.Elective.Item2 })
.Take(2)
IMHO,解决这个问题的最干净的方法是实现一个自定义相等比较器,它提供了解决当前问题所需的相等语义:
class IgnoreElectiveOrderStudentEqualityComparer
: IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
=> (x.ElectiveOne == y.ElectiveOne &&
x.ElectiveTwo == y.ElectiveTwo) ||
(x.ElectiveOne == y.ElectiveTwo &&
x.ElectiveTwo == y.ElectiveOne);
public int GetHashCode(Student obj)
=> obj.ElectiveOne.GetHashCode() ^
obj.ElectiveTwo.GetHashCode();
}
诀窍是让两门选修课的顺序都是无关紧要的,所以alpha-first总是第一(因此,如果你有(数学,艺术),它与(艺术,数学)是一样的:
如果您不想要具有计数的对象,可以添加一个select键。项目1和4不是相同的吗?您是对的——我错过了阅读您的代码,当然XOR是一个顺序无关的运算符。(出于某种原因,我看到了串联。)
var mostPopular =
students.GroupBy(s => s,
new IgnoreElectiveOrderStudentEqualityComparer())
.OrderByDescending(g => g.Count())
.Select(g => new
{
g.Key.ElectiveOne,
g.Key.ElectiveTwo
})
.Take(2);
var most_pop = cStudent.GroupBy(x => { if (string.Compare(x.elective1,x.elecitive2) > 0)
return(new Tuple(x.elective1.x.elective2);
else
return(new Tuple(x.elective2,x.elective1);
},
(b, a) => new { Key = b, Count = a.Count() })
.OrderyByDesending(x => x.Count).Take(2);