Why does using anonymous type work and using an explicit type not in a GroupBy?

孤街浪徒 提交于 2019-11-30 09:06:16

In the first version you are creating an anonymous type with a single property named GroupKey. Anonymous types in C# use structural equality so the equality of the values comes down to the equality of the keys. This causes them to be properly grouped together.

In the second case you are using a custom type named GroupingKey. It appears this uses the default or referential equality. Hence none of the instances are considered equal and hence they get put into different groups.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!