C#Linq Group by与Distinct不工作
我已经阅读了很多关于如何在列表对象上使用LINQ语句进行分组的文章,但是下面的代码无法对任何内容进行分组,即使所有变量都具有完全相同的值。有人能告诉我为什么这可能不起作用吗?它是否与LINQ延迟执行有关?我尝试了一个示例,其中包含了第二个select语句,但没有任何帮助。我正在传入一个C#Linq Group by与Distinct不工作,c#,linq,C#,Linq,我已经阅读了很多关于如何在列表对象上使用LINQ语句进行分组的文章,但是下面的代码无法对任何内容进行分组,即使所有变量都具有完全相同的值。有人能告诉我为什么这可能不起作用吗?它是否与LINQ延迟执行有关?我尝试了一个示例,其中包含了第二个select语句,但没有任何帮助。我正在传入一个列表对象,该对象有两个相同的项,一个不相同,因此当执行此语句时,我应该只收到两个项 代码中的实际值: Type = 1, Capacity = 50, Goal = "Teach algebra",
列表
对象,该对象有两个相同的项,一个不相同,因此当执行此语句时,我应该只收到两个项
代码中的实际值:
Type = 1, Capacity = 50, Goal = "Teach algebra",
Attachments = "", Hours = 1, TypeDesignation = 3,
TypeControl = 1, Supplies = 0
TypeControl and Supplies are both objects themselves. Supplies is empty.
TypeControl contains Id = 23, Text = "Math", Active = 1
var test = newclass.GroupBy(n => new
{
n.Type,
n.Capacity,
n.Goal,
n.Attachments,
n.Hours,
n.TypeDesignation,
n.TypeControl,
n.Supplies
}).Distinct().Select(n => new MyClass()
{
Type = n.Key.Type,
Capacity = n.Key.Capacity,
Goal = n.Key.Goal ,
Attachments = n.Key.Attachments,
Hours = n.Key.Hours ,
TypeDesignation = n.Key.TypeDesignation,
TypeControl = n.Key.TypeControl,
Supplies = n.Key.Supplies
});
我的问题的答案是,里面的所有对象实际上是不相等的,因为我有一个子对象,在分组过程中,一个对象被读取为null,而另一个对象被读取为0,所以它们永远不能组合在一起,但当我用intellisense查看它们时,它们总是都说null。我通过循环浏览列表并使用比较器方法深入到子对象并找到差异,解决了这个问题
VerifyDuplicates duplicates = new VerifyDuplicates();
List<MyClass> newClass = new List<MyClass>();
classes = classes.OrderBy(s => s.Type).ToList();
List<int> idsToDelete = new List<int>();
for (int i = 0; i < classes.Count; i++)
{
if (i + 1 < classes.Count)
{
var x = duplicates.Compare(classes[i], classes[i + 1]);
if (x == 1)
idsToDelete.Add(classes[i+1].Id);
}
}
newClass = classes.Where(n => !idsToDelete.Contains(n.Id)).Select(n => n).ToList();
public class VerifyDuplicates : Comparer<MyClass>
{
public override int Compare(MyClass x, MyClass y)
{
int p = 0;
if(x != null && y != null)
{
if(x.Type.Equals(y.Type)) { p += 1; }
if(x.Attachments.Equals(y.Attachments)) { p += 1; }
if(x.Capacity.Equals(y.Capacity)) { p += 1; }
if(x.Goal.Equals(y.Goal)) { p += 1; }
if(x.Hours.Equals(y.Hours)) { p += 1; }
if(x.TypeDesignation.Equals(y.TypeDesignation)) { p += 1; }
if(x.TypeControl != null && y.TypeControl != null)
if(x.TypeControl[0].Equals(y.TypeControl[0])) { p += 1; }
if(x.Supplies != null && y.Supplies!= null)
if (x.Supplies.Equals(y.Supplies)) { p += 1; }
return p;
}
}
}
VerifyDuplicates duplicates=新的VerifyDuplicates();
List newClass=newlist();
classes=classes.OrderBy(s=>s.Type.ToList();
List idsToDelete=新列表();
for(int i=0;i!idsToDelete.Contains(n.Id)).Select(n=>n.ToList();
公共类验证副本:比较器
{
公共覆盖整型比较(MyClass x,MyClass y)
{
int p=0;
如果(x!=null&&y!=null)
{
如果(x.Type.Equals(y.Type)){p+=1;}
如果(x.Attachments.Equals(y.Attachments)){p+=1;}
如果(x.Capacity.等于(y.Capacity)){p+=1;}
如果(x.Goal.Equals(y.Goal)){p+=1;}
如果(x.Hours.等于(y.Hours)){p+=1;}
如果(x.TypeDesignation.Equals(y.TypeDesignation)){p+=1;}
if(x.TypeControl!=null&&y.TypeControl!=null)
如果(x.TypeControl[0]。等于(y.TypeControl[0]){p+=1;}
如果(x.Supplies!=null和&y.Supplies!=null)
如果(x.Supplies.Equals(y.Supplies)){p+=1;}
返回p;
}
}
}
为什么在GroupBy
之后使用Distinct
?每个组的键都是唯一的。@TimSchmelter我读过的一篇帖子建议添加Distinct,所以这是我尝试过的事情之一。当您说每个组的键是唯一的时,您指的是select语句中的键?我删除了Distinct并重新测试,但我仍然收到了三个项目。@ElaineK AGroupBy
将生成一个IEnumerable
,其中每个I分组
都有一个基于您提供给GroupBy
的lambda的键。该键将是唯一的,因此每个i分组
都是唯一的,Distinct
将无效。@elaine:是的,GroupBy
返回组,您的匿名类型是组的键,这意味着每个组的类型、容量、目标、附件、小时数、类型指定、类型控制、,耗材
是唯一的。我想您只是想选择键?