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

编程入门 行业动态 更新时间:2024-10-24 08:27:05
本文介绍了Linq to SQL:选择最新的DISTINCT条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

给出一个包含以下列的表:[AuditEntityId] [UserName] [CaseID]

Given a table with the columns: [AuditEntityId] [UserName] [CaseID]

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

I want a distinct list of [CaseID]'s for a specific [UserName] that has the highest [AuditEntityId].

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

Basically, I want the last five cases that a user worked on, in the order from latest to oldest.

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

I am achieving the distinct by grouping on [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:

This achieves the goal of giving me a distinct list of cases a user worked on, but the .OrderByDescending is totally ignored. The top linq statement is transformed into the following SQL:

SELECT TOP (5) [Limit1].[AuditEntityId] AS [AuditEntityId], [Limit1].[Reference] AS [Reference], [Limit1].1697291609 AS 1697291609, [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].1697291609 AS 1697291609, [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 进行排序后对结果进行排序,这不会为我提供最新的审核条目.

The provided SQL does not have a ORDER in it at all. I can move the .OrderByDescending to after the .GroupBy(a => a.CaseID).Select(a => a.FirstOrDefault()), but then it orders the result after the TOP (1) has been SELECTed, which doesn't give me the latest audit entries.

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

I also tried making use of MoreLinq's .DistinctBy, but with this the .OrderByDescending still does not work as intended:

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

推荐答案

您需要订购结果集.试试

You need to order your result set. Try

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

在 AuditEntityId 排序后的 CaseID分组中,随后进行其他操作时,该订单 OrderBy 不会对结果产生影响设置.

When you Group By CaseID after Order By AuditEntityId, followed by other operations, that order OrderBy does not have an effect on the result set.

修改

我不知道确切的模式.但是,通过我想要具有最高[AuditEntityId]的特定[UserName]的[CaseID]的不同列表" ,您可以尝试

Without knowing the exact schema, I can't be certain. But going by "I want a distinct list of [CaseID]'s for a specific [UserName] that has the highest [AuditEntityId]", you can try this

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

更多推荐

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

本文发布于:2023-10-14 21:53:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条目   最新   Linq   SQL   DISTINCT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!