Jeff Yates
Jeff Yates

Reputation: 62387

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.

I've always wondered why there is no distinct keyword in the C# LINQ keyword set so that I could write:

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(); a distinct keyword would feel more natural.

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. I could even imagine a distinct by keyword combination so that a comparison operator could be provided inline to the query.

Upvotes: 17

Views: 2978

Answers (3)

James King
James King

Reputation: 6343

Reword: distinct is a set operator... set operators don't take lambdas as parameters. 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. .Distinct() doesn't take a lambda, so is clear when you call it directly.

A good read on the subject:
Link

Upvotes: 5

Donut
Donut

Reputation: 112825

Charlie Calvert has a blog post ("Using Distinct and Avoiding Lambdas") discussing the issue. From the top of the post:

  1. Most query operators such as Select(), Where() and GroupBy() take something called a lambda as a parameter. 2. Lambdas are difficult to write. 3. Query expressions were created in large part to allow developers to use LINQ without having to learn the complex syntax associated with lambdas. 4. A few query operators, such as Distinct(), do not take lambdas as parameters. 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.

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. 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.

TL;DR: There's no distinct keyword for simplicity's sake, since distinct does not take a lambda expression.

Upvotes: 10

Dario
Dario

Reputation: 49218

In VB, there actually is.

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.

Upvotes: 14

Related Questions