给定一个带有列的表:[AuditEntityId] [UserName] [CaseID]

我想要具有最高[AuditEntityId]的特定[UserName]的[CaseID]的不同列表。

基本上,我希望用户处理的最后五个案例,从最新到最早的顺序。

我通过对[CaseID]进行分组来实现与众不同:

var lastItems = baseController.db.AuditEntities
                                 .OrderByDescending(a => a.AuditEntityId)
                                 .GroupBy(a => a.CaseID)
                                 .Select(a => a.FirstOrDefault())
                                 .Where(a => a.CaseID != null && a.CaseID != 0)
                                 .Where(a => a.UserName == filterContext.HttpContext.User.Identity.Name)
                                 .Take(5)
                                 .ToList();


这实现了为我提供用户处理过的不同案例列表的目标,但是.OrderByDescending完全被忽略了。顶部linq语句转换为以下SQL:

SELECT TOP (5)
    [Limit1].[AuditEntityId] AS [AuditEntityId],
    [Limit1].[Reference] AS [Reference],
    [Limit1].[Timestamp] AS [Timestamp],
    [Limit1].[EntityName] AS [EntityName],
    [Limit1].[UserName] AS [UserName],
    [Limit1].[Action] AS [Action],
    [Limit1].[ComplaintId] AS [ComplaintId],
    [Limit1].[CaseID] AS [CaseID],
    [Limit1].[AuditReferencingStart] AS [AuditReferencingStart],
    [Limit1].[AuditReferencingEnd] AS [AuditReferencingEnd]
FROM
    (SELECT DISTINCT
        [Extent1].[CaseID] AS [CaseID]
    FROM
        [dbo].[AuditEntity] AS [Extent1] ) AS [Distinct1]
    CROSS APPLY (SELECT TOP (1)
        [Extent2].[AuditEntityId] AS [AuditEntityId],
        [Extent2].[Reference] AS [Reference],
        [Extent2].[Timestamp] AS [Timestamp],
        [Extent2].[EntityName] AS [EntityName],
        [Extent2].[UserName] AS [UserName],
        [Extent2].[Action] AS [Action],
        [Extent2].[ComplaintId] AS [ComplaintId],
        [Extent2].[CaseID] AS [CaseID],
        [Extent2].[AuditReferencingStart] AS [AuditReferencingStart],
        [Extent2].[AuditReferencingEnd] AS [AuditReferencingEnd]
    FROM
        [dbo].[AuditEntity] AS [Extent2]
    WHERE
        ([Distinct1].[CaseID] = [Extent2].[CaseID]) OR (([Distinct1].[CaseID] IS NULL) AND ([Extent2].[CaseID] IS NULL)) ) AS [Limit1]
WHERE
    ([Limit1].[CaseID] IS NOT NULL) AND ( NOT ((0 = [Limit1].[CaseID]) AND ([Limit1].[CaseID] IS NOT NULL))) AND (([Limit1].[UserName] = @p__linq__0))


提供的SQL根本没有ORDER。我可以将.OrderByDescending移到.GroupBy(a => a.CaseID).Select(a => a.FirstOrDefault())之后,但是在TOP (1)被设置为SELECT之后,它将对结果进行排序,这不会为我提供最新的审核条目。

我也尝试使用MoreLinq.DistinctBy,但是.OrderByDescending仍然无法按预期工作:

var lastItems = baseController.db.AuditEntities
                                 .Where(a => a.CaseID != null && a.CaseID != 0 && a.UserName == filterContext.HttpContext.User.Identity.Name)
                                 .DistinctBy(a => a.CaseID)
                                 .OrderBy(a => a.AuditEntityId)
                                 .Take(5)
                                 .ToList();

最佳答案

您需要订购结果集。尝试

var lastItems = baseController.db.AuditEntities
                             .GroupBy(a => a.CaseID)
                             .Select(a => a.FirstOrDefault())
                             .Where(a => a.CaseID != null && a.CaseID != 0)
                             .Where(a => a.UserName == filterContext.HttpContext.User.Identity.Name)
                             .OrderByDescending(a => a.AuditEntityId)
                             .Take(5)
                             .ToList();


当您在Group By CaseID之后执行Order By AuditEntityId并执行其他操作时,该OrderBy顺序不会对结果集产生影响。

编辑

不知道确切的架构,我无法确定。但是,通过“我想要具有最高[AuditEntityId]的特定[UserName]的[CaseID]的不同列表”,您可以尝试执行此操作

.db.AuditEntities
    .Where(a => a.CaseID != null
                && a.CaseID != 0
                && a.UserName == filterContext.HttpContext.User.Identity.Name)
    .GroupBy(a => a.CaseID)
    .OrderByDescending(grp => grp.Max(g => g.AuditEntityId))
    .Take(5)
    .Select(a => a.FirstOrDefault())
    .ToList();

10-06 03:06