LINQ;如何获得加入最大日期的记录?

时间:2012-05-08 03:24:48

标签: linq max

如何在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
            };

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

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
            };

2 个答案:

答案 0 :(得分:15)

这或多或少是字面翻译

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
            };

答案 1 :(得分: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
    };