简体   繁体   English

获取不同的结果(IEqualityComparer比较器)

[英]Getting the results of distinct( IEqualityComparer comparer)

As a LINQ-beginner I wonder why nobody mentioned in Implementing IEqualityComparer<T> for comparing arbitrary properties of any class (including anonymous) that the query actually has to be carried out to get the results. 作为LINQ初学者,我想知道为什么在实现IEqualityComparer <T>中没有人提到要比较实际上必须执行查询才能获得结果的任何类(包括匿名类)的任意属性 In other words, just calling 换句话说,只是打电话
IEnumerable<Person> people = ... ; // some database call here var distinctPeople = people.Distinct(new PropertyComparer<Person>("FirstName"));
will not trigger the execution of the specific Equals(Tx, Ty) and GetHashCode(T obj) methods in the PropertyComparer. 不会触发PropertyComparer中特定Equals(Tx, Ty)GetHashCode(T obj)方法的执行。

The message "Expanding the Results View will enumerate the IEnumerable" (in the Debugger) gave me the hint. 消息“扩展结果视图将枚举IEnumerable”(在调试器中)给了我提示。 Now, could I proceed with something like foreach (var dp in distinctPeople) to get the results? 现在,我可以继续进行诸如foreach (var dp in distinctPeople)以获得结果吗?

This has nothing at all to do with the IEqualityComparer . 这与IEqualityComparer完全无关。 It is entirely based on the method you're providing it to, in this case, Distinct . 它完全基于您提供给Distinct Distinct , along with all of the LINQ methods that return an IEnumerable , defer execution as much as possible, only performing the work that they need to compute the results when they actually need to do so. Distinct与所有返回IEnumerable的LINQ方法一起,尽可能地推迟执行,仅在实际需要时执行他们需要计算结果的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM