Here is my LINQ query:
(from o in entities.MyTable
orderby o.MyColumn
select o.MyColumn).Distinct();
Here is the result:
{"a", "c", "b", "d"}
Here is the generated SQL:
SELECT
[Distinct1].[MyColumn] AS [MyColumn]
FROM ( SELECT DISTINCT
[Extent1].[MyColumn] AS [MyColumn]
FROM [dbo].[MyTable] AS [Extent1]
) AS [Distinct1]
Is this a bug? Where's my ordering, damnit?
You should sort after Distinct
as it doesn't come with any guarantee about preserving the order:
entities.MyTable.Select(o => o.MyColumn).Distinct().OrderBy(o => o);
This question discusses the rules for Linq to Objects: Preserving order with LINQ
In the database, even fewer operations preserve order. No one anywhere preserves order when Distinct'ing (as generally a Hash algorithm is used).
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