Afternoon,
I would like to know how i would do this query in LINQ, can anyone please provide a hand.
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
Update
I have updated my code as per Andrei’s solution, however there is an issue converting the ID’s please can you see what it could be?
[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();
}
}
I have created this to set up the response, not sure if this is needed. Cheers.
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; }
}
1 Answer