如何在LINQ中执行此查询

时间:2012-07-30 14:00:21

标签: linq-to-sql

下午,

我想知道如何在LINQ中执行此查询,任何人都可以提供帮助。

 SELECT   Id, ExportDate,
         (SELECT        TOP (1) Id
          FROM            Orders
          WHERE        (PickupListId = PickingLists.Id)) AS StartOrderId,
         (SELECT        TOP (1) Id
          FROM            Orders AS Orders_1
          WHERE        (PickupListId = PickingLists.Id)
          ORDER BY Id DESC) AS EndOrderId,
         (SELECT        COUNT(Id) AS Expr1
          FROM            Orders AS Orders_2
          WHERE        (PickupListId = PickingLists.Id)) AS NumberOfOrders
 FROM     PickingLists
 ORDER BY ExportDate DESC

更新 我根据Andrei的解决方案更新了我的代码,但转换ID的问题请你能看到它的内容吗?

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<GetPickingLists> GetPickingLists()
    {
        using (aboDataDataContext dc = new aboDataDataContext())
        {
            var query = from list in dc.pickingLists
                        orderby list.ExportDate descending
                        select new GetPickingLists
                        {
                            plId = list.Id,
                            plDate = list.ExportDate,
                            orderStart = Convert.ToInt32(dc.amzOrders.Where(order => order.pickupListId == list.Id).FirstOrDefault()),
                            orderEnd = Convert.ToInt32(dc.amzOrders.Where(order => order.pickupListId == list.Id).OrderByDescending(order => order.id).FirstOrDefault()),
                            orderCount = dc.amzOrders.Where(order => order.pickupListId == list.Id).Count(),
                        };

            return query.ToList();
        }
    }

我创建了这个以设置响应,不确定是否需要。欢呼声。

 public class GetPickingLists
{
    public int plId { get; set; }
    public int orderStart { get; set; }
    public int orderEnd { get; set; }
    public int orderCount { get; set; }
    public DateTime plDate { get; set; }
}

2 个答案:

答案 0 :(得分:1)

试试这样:

var query = db.PickingLists
            .OrderByDescending(pl => pl.ExportDate)
            .Select(pl => new GetPickingLists { 
               plId = pl.Id, 
               plDate = pl.ExportDate, 
               orderStart = db.Orders.FirstOrDefault(o => o.PickupListId = pl.Id).Id, 
               orderEnd = db.Orders.LastOrDefault(o => o.PickupListId = pl.Id).Id, 
               orderCount= db.Orders.Count(o => o.PickupListId = pl.Id) 
            });

答案 1 :(得分:1)

var query = from list in dbContext.PickingLists
            orderby list.ExportDate descending
            select new
            {
                list.Id,
                list.ExportDate,
                StartOrderId = dbContext.Orders.Where(order => order.PickupListId == list.Id).FirstOrDefault().Id,
                EndOrderId = dbContext.Orders.Where(order => order.PickupListId == list.Id).OrderByDescending(order => order.Id).FirstOrDefault().Id,
                NumberOfOrders = dbContext.Orders.Where(order => order.PickupListId == list.Id).Count(),
            }