Linq to SQL:选择最新的DISTINCT条目

时间:2016-01-22 06:59:00

标签: c# sql-server linq linq-to-sql distinct

给定一个包含列的表:[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();

1 个答案:

答案 0 :(得分:4)

您需要订购结果集。尝试

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();