Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 911
Jeff Yates Avatar asked Nov 12 '10 17:11

Jeff Yates


People also ask

How Distinct Works in LINQ?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).

How do I get distinct on a single column in LINQ?

distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table). I know writing basic query using distinct as followed: var query = (from r in table1 orderby r. Text select r).


1 Answers

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.

like image 80
Dario Avatar answered Sep 25 '22 13:09

Dario