LINQ:按汇总分组,但仍从最近的行中获取信息吗? [英] LINQ: Group by aggregate but still get information from the most recent row?

查看:72
本文介绍了LINQ:按汇总分组,但仍从最近的行中获取信息吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个保存运输历史的表格.我想编写一个查询,计算每个用户的发货量,并从该用户表中的最新条目中获取发货名称.

Let's say I have a table that holds shipping history. I'd like to write a query that counts the amount of shipments per user and gets the shipping name from the most recent entry in the table for that user.

为简单起见,表结构: 货件编号 会员ID 发货名称 ShippingDate

Table structure for simplicity: ShipmentID MemberID ShippingName ShippingDate

如何编写LINQ C#查询来做到这一点?

How do I write a LINQ C# query to do this?

推荐答案

听起来像可能需要这样的东西:

It sounds like might want something like:

var query = from shipment in context.ShippingHistory
            group shipment by shipment.MemberID into g
            select new { Count = g.Count(),
                     MemberID = g.Key,
                     MostRecentName = g.OrderByDescending(x => x.ShipmentDate)
                                       .First()
                                       .ShipmentName };

这篇关于LINQ:按汇总分组,但仍从最近的行中获取信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆