Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按不同的整数列表分组 MyObject() { 弦乐部; 列表id; 对象对象对象; }_C#_Linq - Fatal编程技术网

C# 按不同的整数列表分组 MyObject() { 弦乐部; 列表id; 对象对象对象; }

C# 按不同的整数列表分组 MyObject() { 弦乐部; 列表id; 对象对象对象; },c#,linq,C#,Linq,使用LINQ,如何返回按如下方式组织的上述对象的列表: 按[department and EQUAL id list]对所有obj对象进行分组。如果列表包含相同的数字,而不一定是相同的顺序(一组),则认为该列表是相等的。GroupBy具有接受自定义IEqualityComparer。当dept相等且id设置为相等时,编写一个将两个对象视为相等的对象,并将其作为参数传递 实现集合相等的一种方便方法是编写 MyObject() { String dept; List<int

使用
LINQ
,如何返回按如下方式组织的上述对象的列表:


按[department and EQUAL id list]对所有obj对象进行分组。如果列表包含相同的数字,而不一定是相同的顺序(一组),则认为该列表是相等的。

GroupBy
具有接受自定义
IEqualityComparer
。当
dept
相等且
id
设置为相等时,编写一个将两个对象视为相等的对象,并将其作为参数传递

实现集合相等的一种方便方法是编写

MyObject()
{
     String dept;
     List<int> id;
     Object obj;
}
尽管这会导致效率低下,而且如果要进行大量比较,这可能不是最好的办法。

如果效率有问题,您可以将每个对象的
哈希集存储在匿名对象中:

new HashSet(x.id).SetEquals(new HashSet(y.id))
myObjects.Select(x=>new{myObject=x,hashSet=newhashset(x.id)})
.GroupBy(x=>x.hashSet,hashSet.CreateSetComparer())
.SelectMany(x=>x.GroupBy(y=>y.myObject.dept))
如果只想执行一个
GroupBy
,可以将
HashSet
存储在
Tuple
或自定义类中,但随后必须创建自己的
IEqualityComparer

myObjects.Select(x => new { myObject = x, hashSet = new HashSet(x.id) })
         .GroupBy(x => x.hashSet, HashSet<int>.CreateSetComparer())
         .SelectMany(x => x.GroupBy(y => y.myObject.dept))