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.
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).
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With