Hi Walaa,
I'm trying to replicate the following query:
from co in ClientOrders
where co.OrderStatus == (int) OrderStatus.NewOrder
&& co.RequestedFor < DateTime.Now
group co by co.FK_ClientLocationID
into grp
select grp.OrderBy(g => g.RequestedFor).First()
which translates to the following SQL (there's probably a cleaner way of writing it though):
-- Region Parameters
DECLARE @p0 Int = 1
DECLARE @p1 DateTime = '2010-10-05 17:55:32.786'
-- EndRegion
SELECT [t3].[test], [t3].[PK_ClientOrderID], [t3].[FK_ClientID], [t3].[FK_ClientLocationID], [t3].[Name], [t3].[PhoneNumber], [t3].[FK_RegisteredDeviceID], [t3].[FK_PublicUserID], [t3].[SpecialRequests], [t3].[CreatedOn], [t3].[RequestedFor], [t3].[TransmittedOn], [t3].[ResponseReceived], [t3].[OrderStatus], [t3].[EzSurcharge]
FROM (
SELECT [t0].[FK_ClientLocationID]
FROM [client].[ClientOrders] AS [t0]
WHERE ([t0].[OrderStatus] = @p0) AND ([t0].[RequestedFor] < @p1)
GROUP BY [t0].[FK_ClientLocationID]
) AS [t1]
OUTER APPLY (
SELECT TOP (1) 1 AS [test], [t2].[PK_ClientOrderID], [t2].[FK_ClientID], [t2].[FK_ClientLocationID], [t2].[Name], [t2].[PhoneNumber], [t2].[FK_RegisteredDeviceID], [t2].[FK_PublicUserID], [t2].[SpecialRequests], [t2].[CreatedOn], [t2].[RequestedFor], [t2].[TransmittedOn], [t2].[ResponseReceived], [t2].[OrderStatus], [t2].[EzSurcharge]
FROM [client].[ClientOrders] AS [t2]
WHERE ([t1].[FK_ClientLocationID] = [t2].[FK_ClientLocationID]) AND ([t2].[OrderStatus] = @p0) AND ([t2].[RequestedFor] < @p1)
ORDER BY [t2].[RequestedFor]
) AS [t3]
ORDER BY [t3].[RequestedFor]
i.e. I want only the New Orders that are requested prior to the current datetime and I only want the oldest single request per client location.