dotnet / dotnet/EntityFramework.Docs

Misleading example in "Distinct queries" section

Open
#4,242 0 comments 0 reactions 0 assignees View on GitHub
area-query
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

One of the code fragment contains a misleading example:

```
var distinctResults
= context.Triangles
.Select(e => e.Angle1).OrderBy(e => e).Distinct()
.ToList();
```

The performing a `Distinct` right after `OrderBy` is meaningless, as `Distinct` does not preserve the order (as per [docs](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=net-7.0#remarks)).

AFAICT, the `OrderBy` in this code fragment could be removed without affecting the meaning of the queriable/enumerable.
If the generated query actually contains an `ORDER BY`, we are probably missing out on some optimizations.
If the actual results match https://github.com/dotnet/efcore/issues/25696 the example is misleading.

---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: 642054ea-366d-f8a2-4495-3e491b78fe1c
* Version Independent ID: e8884181-676d-4846-b2b7-ad0a3c7c1a3f
* Content: [What's New in EF Core 6.0](https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/whatsnew#distinct-queries)
* Content Source: [entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md](https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/what-is-new/ef-core-6.0/whatsnew.md)
* Product: **entity-framework**
* Technology: **entity-framework-core**
* GitHub Login: @ajcvickers
* Microsoft Alias: **avickers**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.