简体   繁体   English

EF Core 3.1 / MySQL - 在 Distinct() 之后忽略 OrderByDescending

[英]EF Core 3.1 / MySQL - OrderByDescending is ignored after Distinct()

I run this query我运行这个查询

       var query = await _db.Production
                .OrderByDescending(q => q.Id)
                .Where(q => q.Industry > 0)
                .Select(m => m.FromCompany).Distinct().Take(10).ToListAsync()

And get the following并得到以下

 Executed DbCommand (64ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30'] SELECT DISTINCT `j`.`from_company` FROM `production` AS `j` WHERE (`j`.`Industry` > 0) LIMIT @__p_0

However, I expected to see然而,我希望看到

ORDER BY production .production订购。 id DESC id

Which I did at the begging我在乞讨时做的

.OrderByDescending(q => q.Id) .OrderByDescending(q => q.Id)

How can I order after distinct?如何在不同后订购?


Update.更新。

If I put order after distinct, I will not be able to sort by ID: Error CS1061 'int' does not contain a definition for 'Id' and no accessible extension method 'Id' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)如果我在distinct之后下订单,我将无法按ID排序:错误CS1061'int'不包含'Id'的定义并且没有可访问的扩展方法'Id'接受'int'类型的第一个参数可能是找到(您是否缺少 using 指令或程序集引用?)

I run this query我运行此查询

       var query = await _db.Production
                .OrderByDescending(q => q.Id)
                .Where(q => q.Industry > 0)
                .Select(m => m.FromCompany).Distinct().Take(10).ToListAsync()

And get the following并获得以下内容

 Executed DbCommand (64ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30'] SELECT DISTINCT `j`.`from_company` FROM `production` AS `j` WHERE (`j`.`Industry` > 0) LIMIT @__p_0

However, I expected to see但是,我希望看到

ORDER BY production .production订购。 id DESC id DESC

Which I did at the begging我在乞讨中做了

.OrderByDescending(q => q.Id) .OrderByDescending(q => q.Id)

How can I order after distinct?区分后如何订购?


Update.更新。

If I put order after distinct, I will not be able to sort by ID: Error CS1061 'int' does not contain a definition for 'Id' and no accessible extension method 'Id' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)如果我将订单放在非重复之后,我将无法按ID排序:错误CS1061'int'不包含'Id'的定义,也没有可能接受类型为'int'的第一个参数的可访问扩展方法'Id'找到(您是否缺少using指令或程序集引用?)

PS I've reported this "issue" PS我已经报告了这个“问题”

Now it is "fixed" and people will get warnings in .net 6现在它已“修复”,人们将在 .net 6 中收到警告

https://github.com/dotnet/efcore/pull/24160 https://github.com/dotnet/efcore/pull/24160

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM