简体   繁体   English

为什么LINQ不包含`distinct`关键字?

[英]Why doesn't LINQ include a `distinct` keyword?

NOTE: Before you read on or provide an answer, I know about Enumerable.Distinct , I am asking about specific language support for that method, not about the method itself. 注意:在您阅读或提供答案之前,我知道Enumerable.Distinct ,我问的是该方法的特定语言支持,而不是方法本身。

I've always wondered why there is no distinct keyword in the C# LINQ keyword set so that I could write: 我一直想知道为什么C#LINQ关键字集中没有distinct关键字,所以我可以写:

var items = distinct from x in y
            select x;

or 要么

var items = from x in y
            select distinct x;

Anybody know why this wasn't included or why it would be a bad idea to include it? 任何人都知道为什么不包括它或为什么包含它是一个坏主意? It just feels cumbersome to me that I have to wrap the query just to call Distinct() ; 对我来说,我只需要调用查询来调用Distinct() ; a distinct keyword would feel more natural. 一个distinct关键字会感觉更自然。

NOTE: I know that the Distinct method has overrides to provide a comparer if that is required, but a keyword that uses the default comparer would be great. 注意:我知道如果需要, Distinct方法会覆盖提供比较器,但使用默认比较器的关键字会很棒。 I could even imagine a distinct by keyword combination so that a comparison operator could be provided inline to the query. 我甚至可以想象一个distinct by关键字组合,以便可以为查询提供内联的比较运算符。

In VB, there actually is . 在VB中,确实存在

Dim l = From x In {1, 2, 3, 2, 4, 2} Distinct Select x

I don't suspect there has been some active decision against distinct for C#, it's just has not been implemented. 我不怀疑有针对某些活性决定distinct的C#,它只是一直没有执行。

Charlie Calvert has a blog post ("Using Distinct and Avoiding Lambdas") discussing the issue. Charlie Calvert有一篇博客文章(“使用Distinct and Avoiding Lambdas”)讨论这个问题。 From the top of the post: 从帖子的顶部:

  1. Most query operators such as Select() , Where() and GroupBy() take something called a lambda as a parameter. 大多数查询运算符(如Select()Where()GroupBy()将一个名为lambda的东西作为参数。
  2. Lambdas are difficult to write. Lambdas很难写。
  3. Query expressions were created in large part to allow developers to use LINQ without having to learn the complex syntax associated with lambdas. 查询表达式的创建在很大程度上允许开发人员使用LINQ而无需学习与lambdas相关的复杂语法。
  4. A few query operators, such as Distinct() , do not take lambdas as parameters. 一些查询运算符(例如Distinct()不将lambdas作为参数。 As a result, they are easy to call. 结果,他们很容易打电话。
  5. Query expressions were therefore not created for operators such as Distinct() that do not take lambdas. 因此,不为诸如Distinct()运算符创建不使用lambda的查询表达式。

And also, from further down in the post: 而且,从帖子的更下方:

Query operators are method calls. 查询运算符是方法调用。 In other words, there are methods in the LINQ API called Select() , Group() , Distinct() , etc. We don't usually call these methods directly because they take lambdas as parameters, and many people find that lambdas are hard to understand. 换句话说,LINQ API中有一些方法叫做Select()Group()Distinct()等。我们通常不会直接调用这些方法,因为它们将lambdas作为参数,很多人发现lambdas很难了解。 To help developers avoid the complex task of writing lambdas, the team invented query expressions, which are a "syntactic sugar" that sit on top of lambdas. 为了帮助开发人员避免编写lambda的复杂任务,该团队发明了查询表达式,这是一种位于lambdas之上的“语法糖”。

TL;DR: There's no distinct keyword for simplicity's sake, since distinct does not take a lambda expression. TL; DR:为了简单起见,没有distinct关键字,因为distinct不接受lambda表达式。

Reword: distinct is a set operator... set operators don't take lambdas as parameters. Reword: distinct是一个set运算符... set运算符不会将lambdas作为参数。 The c# team decided to give you shortcuts to methods that take lambdas, such as Select() and Group() , because they felt that lambdas can be confusing to people just starting out. c#团队决定为你提供使用lambdas的方法的快捷方式,比如Select()Group() ,因为他们觉得lambdas可能会让刚开始的人感到困惑。 .Distinct() doesn't take a lambda, so is clear when you call it directly. .Distinct()不接受lambda,所以当你直接调用它时就很清楚了。

A good read on the subject: 关于这个主题的好读物:
http://blogs.msdn.com/b/charlie/archive/2006/11/19/linq-farm-group-and-distinct.aspx http://blogs.msdn.com/b/charlie/archive/2006/11/19/linq-farm-group-and-distinct.aspx

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

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