【问题标题】:LINQ; how to get record with max date with join?LINQ;如何通过加入获得最大日期的记录?
【发布时间】:2012-05-16 13:18:42
【问题描述】:

如何在 LINQ 中做到这一点?

select
    *
from customer c
left join order o on o.CustomerID = c.CustomerID
where o.OrderDate = (select MAX(OrderDate) from order where CustomerID = o.CustomerID )

不用担心重复,因为每天只有一个订单。

我已经了解了 LINQ 中的左连接,但不确定如何或在何处放置子查询。

var query = from customer in clist
            from order in olist
                .Where(o => o.CustomerID == customer.CustomerID)
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                Product = order != null ? order.Product : string.Empty
            };

最终解决方案:

var query = from customer in clist
            from order in olist
            .Where(o => o.CustomerID == customer.CustomerID && o.OrderDate ==
                olist.Where(o1 => o1.CustomerID == customer.CustomerID).Max(o1 => o1.OrderDate)
            )
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                order.Product,
                order.OrderDate
            };

没有任何 lambda 的另一种解决方案

var query = from customer in clist
            from order in olist 
            where order.CustomerID == customer.CustomerID && order.OrderDate == 
                (from o in olist 
                 where o.CustomerID == customer.CustomerID 
                 select o.OrderDate).Max()
            select new {
                customer.CustomerID,
                customer.Name,
                customer.Address,
                order.Product,
                order.OrderDate
            };

【问题讨论】:

    标签: linq max


    【解决方案1】:

    这对你有什么作用?

    var query =
        from customer in clist
        join order in olist
            on customer.CustomerID equals order.CustomerID
            into orders
        select new
        {
            customer.CustomerID,
            customer.Name,
            customer.Address,
            Product = orders
                .OrderByDescending(x => x.OrderDate)
                .Select(x => x.Product)
                .FirstOrDefault() ?? String.Empty
        };
    

    【讨论】:

      【解决方案2】:

      这或多或少是直译

      var query = from customer in clist
                  from order in olist
                      .Where(o => o.CustomerID == customer.CustomerID &&
                                  o.OrderDate == olist
                                      .Where(o => o.CustomerID == customer.CustomerID)
                                      .Select(o => o.OrderDate).Max())
                  select new {
                      customer.CustomerID,
                      customer.Name,
                      customer.Address,
                      Product = order != null ? order.Product : string.Empty
                  };
      

      但我会改写成

      var query = from customer in clist
                  from order in olist
                      .Where(o => o.CustomerID == customer.CustomerID)
                      .OrderByDescending(o => o.OrderDate).Take(1)
                  select new {
                      customer.CustomerID,
                      customer.Name,
                      customer.Address,
                      Product = order != null ? order.Product : string.Empty
                  };
      

      【讨论】:

      • 谢谢,我基于字面翻译的最终解决方案。
      猜你喜欢
      • 2019-08-20
      • 1970-01-01
      • 2020-11-19
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多