简体   繁体   English

实体框架忽略OrderByDescending

[英]Entity Framework ignoring OrderByDescending

In my SQL Server database I have the following hierarchy 在我的SQL Server数据库中,我具有以下层次结构

Inventory > Datasets > Resources > Renditions > Conformities 库存>数据集>资源>渲染>符合性

where each is a one to many relationship. 其中每个都是一对多的关系。 I wanted to get the id of the three datasets with the most recently updated conformity. 我想获取具有最新更新一致性的三个数据集的ID。 Conformity doesn't have its own date but takes the modified date of the parent rendition. 合格没有自己的日期,而是采用父格式的修改日期。 I therefore created the following query: 因此,我创建了以下查询:

var datasets = _inventoryRepository
    .GetConformitiesIncludeAncestors()
    .OrderByDescending(conformity => conformity.Rendition.Modified)
    .Select(conformity => conformity.Rendition.Resource.DatasetID)
    .Distinct()
    .Take(3);

GetConformitiesIncludeAncestors is simply returning the conformities with includes as follows: GetConformitiesIncludeAncestors只是返回包含的符合性,如下所示:

return _context.Conformities.Include(conformity => conformity.Rendition.Resource.Dataset.Inventory);

but the SQL statement shown when stepping through the code doesn't have an ORDER BY clause. 但是单步执行代码时显示的SQL语句没有ORDER BY子句。

SELECT 
    [Limit1].[DatasetID] AS [DatasetID]
    FROM ( SELECT DISTINCT TOP (3) 
        [Extent3].[DatasetID] AS [DatasetID]
        FROM   [dbo].[Conformity] AS [Extent1]
        INNER JOIN [dbo].[Rendition] AS [Extent2] ON [Extent1].[RenditionID] = [Extent2].[ID]
        INNER JOIN [dbo].[Resource] AS [Extent3] ON [Extent2].[ResourceID] = [Extent3].[ID]
    )  AS [Limit1]

Why is OrderByDescending being ignored? 为什么OrderByDescending被忽略? Entity Framework version is 6.0.1. 实体框架版本为6.0.1。

EDIT: I have a workaround that does the trick, but by querying in a different way. 编辑:我有一种解决方法,可以解决问题,但通过以其他方式查询。 I'm still interested in why the OrderByDescending had no effect so will leave open. 我仍然对为什么OrderByDescending无效没有兴趣,因此将保持打开状态。

My workaround using GroupBy 我使用GroupBy的解决方法

var datasets = _inventoryRepository
    .GetConformitiesIncludeAncestors()
    .GroupBy(conformity => conformity.Rendition.Resource.DatasetID)
    .OrderByDescending(group => group.Max(conformity => conformity.Rendition.Modified))
    .Take(3)
    .Select(group => group.Key);

If you remove the Distinct, you should get similar result like this. 如果删除Distinct,则应获得类似的结果。

var datasets = inventoryRepository
    .GetConformitiesIncludeAncestors()
    .OrderByDescending(comformity => comformity.Rendition.Modified)
    .Select(comformity => comformity.Rendition.Resource.DatasetId)
    //.Distinct()
    .Take(3)

SELECT TOP (3) 
    [Extent3].[DatasetId] AS [DatasetId]
    FROM   [dbo].[Comformities] AS [Extent1]
    INNER JOIN [dbo].[Renditions] AS [Extent2] ON [Extent1].[RenditionId] = [Extent2].[Id]
    INNER JOIN [dbo].[Resources] AS [Extent3] ON [Extent2].[ResourceId] = [Extent3].[Id]
    ORDER BY [Extent2].[Modified] DESC

But after you add the Distinct, it doesn't guarantee the ordering, check the documentation . 但是添加Distinct后,它不能保证订购,请查看文档

The expected behavior is that it returns an unordered sequence of the unique items in source. 预期的行为是它返回源中唯一项的无序序列。

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

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