在 LINQ 中使用 MIN 函数

发布于 2025-01-06 12:21:53 字数 777 浏览 0 评论 0原文

我有一个视图的 SQL 查询。这个视图

SELECT
  [ID]
  [Name],
  [Category],
  MIN(OrderDate) as OrderDate, 
  MIN(DeliveryDate) as DeliveryDate
GROUP BY
  [ID], [Name], [Category]
FROM
  [OrdersView]

我知道这个查询是正确的,因为我的 DBA 给了我它。但我需要将其转换为 LINQ。目前,我有以下问题:

var orders = from o in dataContext.OrdersView
             group o by new { o.ID, o.Name, o.Category } into grouping
             select new Order() {
               ID = grouping.Key.ID,
               Name = grouping.Key.Name,
               Category = grouping.Key.Category,
               OrderDate = ?,
               DeliveryDate = ?
             };

我的问题是我不知道如何在 OrderDate 和 DeliveryDate 上执行 MIN 函数并在 LINQ 查询中使用这些值。不幸的是,我无法更改数据库,因为它已锁定。有人可以指出我正确的方向吗?

谢谢!

I have a SQL Query of a view. This view

SELECT
  [ID]
  [Name],
  [Category],
  MIN(OrderDate) as OrderDate, 
  MIN(DeliveryDate) as DeliveryDate
GROUP BY
  [ID], [Name], [Category]
FROM
  [OrdersView]

I know this query is correct because my DBA gave it to me. But I need to convert it into LINQ. Currently, I have the following:

var orders = from o in dataContext.OrdersView
             group o by new { o.ID, o.Name, o.Category } into grouping
             select new Order() {
               ID = grouping.Key.ID,
               Name = grouping.Key.Name,
               Category = grouping.Key.Category,
               OrderDate = ?,
               DeliveryDate = ?
             };

My problem is I do not know how to execute the MIN function on the OrderDate and DeliveryDate and use those values in my LINQ query. Unfortunately, I cannot change the database because its locked down. Can someone please point me in the right direction?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2025-01-13 12:21:53

OrderDate = grouping.Min(a => a.OrderDate)

DeliveryDate = grouping.Min(a => a.DeliveryDate)

编辑:使用 o< /code> 而不是意外地分组。那应该适合你。

OrderDate = grouping.Min(a => a.OrderDate)

DeliveryDate = grouping.Min(a => a.DeliveryDate)

Edit: Used o instead of grouping accidentally. That should do it for you.

北凤男飞 2025-01-13 12:21:53
var orders = from o in dataContext.OrdersView
         group o by new { o.ID, o.Name, o.Category } into grouping
         let orderDate = grouping.Min(order => order.OrderDate)
         let deliveryDate = grouping.Min(order => order.DeliveryDate)
         select new Order() {
           ID = grouping.Key.ID,
           Name = grouping.Key.Name,
           Category = grouping.Key.Category,
           OrderDate = orderDate,
           DeliveryDate = deliveryDate
         };

将来我建议使用 LINQPad 来测试您的查询。它允许查看生成的 SQL 代码。

var orders = from o in dataContext.OrdersView
         group o by new { o.ID, o.Name, o.Category } into grouping
         let orderDate = grouping.Min(order => order.OrderDate)
         let deliveryDate = grouping.Min(order => order.DeliveryDate)
         select new Order() {
           ID = grouping.Key.ID,
           Name = grouping.Key.Name,
           Category = grouping.Key.Category,
           OrderDate = orderDate,
           DeliveryDate = deliveryDate
         };

In future I'd recommend to use LINQPad for testing your queries. It allows to view generated SQL code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文