[英]C# Linq GroupBy ==> Class vs Anonymous class --- different result
I have the following code:我有以下代码:
var rawGroupBEFORE = sqlRaw.GroupBy(x => new
{
x.ID,
x.P1,
x.P2,
x.P3,
x.P4,
x.P5,
x.P6,
x.P7,
x.P8,
}).ToList();
var rawGroup = sqlRaw.GroupBy(x => new RawDocument
{
Id = x.ID,
P1 = x.P1,
P2 = x.P2,
P3 = x.P3,
P4 = x.P4,
P5 = x.P5,
P6 = x.P6,
P7 = x.P7,
P8 = x.P8,
}).ToList();
And with the data that I have in the rawGroupBEFORE
1 element as result.结果是我在rawGroupBEFORE
1 元素中拥有的数据。 In the rawGroup
gives me 2 elements ..... wrong grouping.在rawGroup
给了我 2 个元素 ..... 错误的分组。 I have created the class RawDocument using the automatic Visual Studio "create missing property" action.我已经使用自动 Visual Studio“创建缺少的属性”操作创建了类 RawDocument。
Any ideas why I should be getting this behavior?任何想法为什么我应该得到这种行为?
The most likely explanation is that the anonymous type uses value equality , while RawDocument
uses reference equality .最可能的解释是匿名类型使用值相等,而RawDocument
使用引用相等。 Since each item will get a new RawDocument
object, none of them will be equal to each other.由于每个项目都将获得一个新的RawDocument
对象,因此它们之间不会彼此相等。
Implement Equality members for RawDocument
, or give the GroupBy a IEqualityComparer.为RawDocument
实现 Equality 成员,或者给 GroupBy 一个 IEqualityComparer。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.