使用自定义比较器调用 Distinct 时返回什么?

发布于 2024-08-19 08:51:42 字数 1051 浏览 3 评论 0原文

如果有一类这样的 Widget:

public class Widget
{
   public double Price { get; set; }
   public string Type { get; set; }
   public int ID { get; set; }
   public string Name { get; set; }
}

并创建它们的列表:

List<Widget> Widgets = new List<Widget>
   {
       new Widget {ID = 1, Name = "One", Price = 3.00, Type = "Gooy"},
       new Widget {ID = 2, Name = "Two", Price = 5.00, Type = "Crispy"},
       new Widget {ID = 2, Name = "Three", Price = 3.00, Type = "Hard"},
       new Widget {ID = 2, Name = "Four", Price = 3.00, Type = "Chewy"},
       new Widget {ID = 2, Name = "Five", Price = 2.50, Type = "Gooy"}
   };

然后我使用自定义比较器调用 IEnumerable.Distinct,如下所示:

IEqualityComparer<Widget> widgetComparer = 
   new LambdaComparer<Widget>((item1, item2) => item1.Price == item2.Price);

Widgets.Distinct(widgetComparer);

然后(正如我所见)应该返回 3 个对象(每个价格类别一个) 。

3.00 的类型是什么(粘性、硬性或耐嚼性)?

它选一个吗? (我试图更好地理解不同的结果,因为我真正的不同并没有给我不同的结果。)

If have a class of Widgets like this:

public class Widget
{
   public double Price { get; set; }
   public string Type { get; set; }
   public int ID { get; set; }
   public string Name { get; set; }
}

And create a list of them:

List<Widget> Widgets = new List<Widget>
   {
       new Widget {ID = 1, Name = "One", Price = 3.00, Type = "Gooy"},
       new Widget {ID = 2, Name = "Two", Price = 5.00, Type = "Crispy"},
       new Widget {ID = 2, Name = "Three", Price = 3.00, Type = "Hard"},
       new Widget {ID = 2, Name = "Four", Price = 3.00, Type = "Chewy"},
       new Widget {ID = 2, Name = "Five", Price = 2.50, Type = "Gooy"}
   };

And then I call IEnumerable.Distinct with a custom comparer like so:

IEqualityComparer<Widget> widgetComparer = 
   new LambdaComparer<Widget>((item1, item2) => item1.Price == item2.Price);

Widgets.Distinct(widgetComparer);

Then (as I see it) there should be 3 objects returned (one for each price category).

What is the Type of the 3.00 one (Gooy, Hard or Chewy)?

Does it pick one? (I am trying to understand distinct better because my real distinct is not giving me distinct results.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梨涡少年 2024-08-26 08:51:42

Distinct 接受比较器确定的任何内容;因此,如果有 3 个不同的对象具有相同的价格,并且您的自定义比较器仅查看价格,那么它应该将它们视为同一对象,具体取决于比较的方式。

HTH。

Distinct takes whatever the comparer determines; so if there are 3 different objects with the same price, and your custom comparer only looks at the price, then it should consider them one and the same, depending on how the comparison takes place.

HTH.

鹿! 2024-08-26 08:51:42

好吧,答案必须是“这取决于”,distinct 应该考虑三个相同并且只保留一个,这通常是第一个,但由于 Distinct 是一种扩展方法,它也将取决于扩展的实现和方法结果“寻找”的方法。这又取决于“接近度”,因此使用相同的比较器对同一 IEnumerable 对象调用 Distinct 可能会产生两个不同的结果。例如,如果两个调用站点上“最近”的 Distinct 实现不同

Well the answer has to be 'it depends' the distinct should consider the three the same and only keep one, which is usually the first but since Distinct is an extension method it Will aldo depend on the implementation of the extension and on method resulution "finding" the method. Which again depends on "nearness" so calling Distinct on the same IEnumerable object with the same Comparer might yield two different results. E.g. If the "nearest" implementation of Distinct differs on the two call sites

鹿港巷口少年归 2024-08-26 08:51:42

这确实是一个哲学问题。您将平等定义为价格相等。因此,根据您的定义,无论 Type 值是什么,它们都是相等的。

所以,答案是:返回哪一个并不重要。他们都是一样的。您不应该关心或预测返回哪一个,并且 Distinct 的实现保留对您进行更改的权利......因为从 a == b == c 的角度来看它们都是相同的。

如果事实证明您确实关心返回哪一个,则说明您错误地定义了相等比较,并且需要重新考虑您的相等函数。

This is really a philosophy question. You are defining equality to mean that the price is equal. Therefore, based on your definition, they are equal regardless of what the Type value is.

So, the answer is: It doesn't matter which one gets returned. They are all the same. You shouldn't care or predict which one gets returned, and the implementation of Distinct reserves the right to change it on you... because they are all the same from an a == b == c perspective.

If it turns out that you do care which one gets returned, than you have defined your equality comparison incorrectly and you need to re-think your equality function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文