c# - LINQ GroupBy,同时保留所有对象字段

标签 c# .net linq lambda

我目前有这个示例数据表:

ID  | Policy ID     |   History ID  | Policy name
1   |   1           |    0          | Test
2   |   1           |    1          | Test
3   |   2           |    0          | Test1
4   |   2           |    1          | Test1

除此之外,我想按策略 ID 和历史 ID (MAX) 进行分组,因此我要保留的记录是 ID 的 2 和 4:

   ID   | Policy ID     |   History ID  | Policy name
    2   |   1           |    1          | Test
    4   |   2           |    1          | Test1

我曾尝试在 LINQ 中执行此操作,但每次都遇到同样的问题。我可以将我的实体分组,但总是分组到一个我必须重新定义属性的组中,而不是让它们远离我的策略对象。如:

var policies = _context.Policies.GroupBy(a => a.intPolicyId)
                                            .Select(group => new {
                                                PolicyID = group.Key,
                                                HistoryID = group.Max(a => a.intHistoryID)
                                            });

这只是简单地列出了一个对象列表,其中包含“Policy ID”和“History ID”。我想要从 Policies 对象返回的所有属性,而不必重新定义它们,因为该对象中有大约 50 多个属性。

我试过:

        var policies = _context.Policies.GroupBy(a => a.intPolicyId)
                                                    .Select(group => new {
                                                        PolicyID = group.Key,
                                                        HistoryID = group.Max(a => a.intHistoryID)
                                                        PolicyObject = group;
                                                    });

但这会出错。

有什么想法吗?

最佳答案

按复合键分组

_context.Policies.GroupBy(a => new {a.intPolicyId, *other fields*}).Select(
    group=> new {
        PolicyId = group.Key.intPolicyId,
        HistoryId = group.Max(intHistoryId),
        *other fields*
    }
);

另一种方式 - 获取历史记录,而不是与其余数据一起返回,就像这样(不会开箱即用,需要一些改进)

var historyIDs = _context.Policies.GroupBy(a=>a.intPolicyId).Select(group => new {
                                            PolicyID = group.Key,
                                            HistoryID = group.Max(a => a.intHistoryID)
                                        });

var finalData = from h in historyIDs
                join p in _context.Policies on h.intPolicyId equals p.intPolicyId
                select new {h.HistoryId, *all other policy fields*}

还有另一种方式,更简单,不需要大量输入:):

var historyIDs = _context.Policies.GroupBy(a=>a.intPolicyId).Select(group => new {
                                            PolicyID = group.Key,
                                            HistoryID = group.Max(a => a.intHistoryID)
                                        });

var finalData = from h in historyIDs
                join p in _context.Policies on h.PolicyId equals p.intPolicyId && h.HistoryId equals p.HistoryId
                select p

基本上它在某种程度上等同于以下 SQL 查询:

select p.*
from Policy p
inner join (
    select pi.policyId, max(pi.historyId)
    from Policy pi
    group by pi.policyId
) pp on pp.policyId = p.policyId and pp.historyId = p.historyId

关于c# - LINQ GroupBy,同时保留所有对象字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13400453/

相关文章:

sql - 具有方法语法的 LINQ 中的 JOIN 和 LEFT JOIN 等效

c# - .NET DICOM库

c# - 扩展基类以包含详细信息?

c# - 获取套接字的本地IP地址

c# - 调用其他命令的 PSCmdlet 由于处理时间长而卡住?

xml - LINQ to XML 在标记之间生成\r\n?

c# - 我可以重构我的 linq select 吗?

c# - 为什么 String.Concat 没有针对 StringBuilder.Append 进行优化?

c# - 网页上的多重访问控制 - C#

c# - 如何使用 LINQ 获取 id 的列表作为 int