このクエリをLinqに取り込もうとしています
SELECT [ID], [Name], LastSync, Phase
FROM [Store]
LEFT JOIN (
SELECT GLog.[StoreId] AS [StoreId], LastSync, Phase
FROM [GGCSyncLog] AS GLog
INNER JOIN (
SELECT MAX(G1.[DateTime]) AS LastSync, G1.[StoreId]
FROM [GGCSyncLog] AS G1
GROUP BY G1.[StoreId]
) AS G2 ON (GLog.[StoreId] = G2.[StoreId]) AND (GLog.[DateTime] = G2.[LastSync])
) AS MostRecentLog ON Store.[ID] = MostRecentLog.[StoreId]
その結果は
ID Name LastSync Phase
1 Sarasota 2010-07-31 5
2 Wellington 2010-07-31 8
3 Tampa International 2013-03-12 8
5 Events NULL NULL
6 PO Holding Store NULL NULL
私のLinqは、LastSyncとPhaseがnullの2行がないことを除いて、正しい結果を返します。何が問題なのですか?
from s in Stores
join gLog in
(from g1 in GGCSyncLogs.DefaultIfEmpty()
join g in
(from g in GGCSyncLogs
group g by g.StoreId into gM
select new {
StoreId = gM.Key, LastSync = gM.Max(gg=>gg.DateTime) })
on new {g1.StoreId, LastSync = g1.DateTime} equals new {g.StoreId, g.LastSync}
select new {g1.StoreId, g.LastSync, g1.Phase})
on s.ID equals gLog.StoreId
select new {s.ID, s.Name,
LastSync = (gLog != null ? (DateTime?)gLog.LastSync : null),
Phase = (gLog != null ? (int?)gLog.Phase : null) }