SQL:
SELECT
u.id,
u.name,
isnull(MAX(h.dateCol), '1900-01-01') dateColWithDefault
FROM universe u
LEFT JOIN history h
ON u.id=h.id
AND h.dateCol<GETDATE()-1
GROUP BY u.Id, u.name
original title: "c# - How do I most elegantly express left join with aggregate SQL as LINQ query"
SQL:
SELECT
u.id,
u.name,
isnull(MAX(h.dateCol), '1900-01-01') dateColWithDefault
FROM universe u
LEFT JOIN history h
ON u.id=h.id
AND h.dateCol<GETDATE()-1
GROUP BY u.Id, u.name
SQL:SELECT u.id、u.name、isnull(MAX(h.dateCol)、 '1900-01-01')dateColWithDefault FROM universe u LEFT JOIN history h ON u.id = h.id AND h.dateCol <GETDATE()-1 GROUP BY u.Id, u.n...
これは翻訳後の要約です。完全な翻訳を表示する必要がある場合は、「翻訳」アイコンをクリックしてください。
A solution, albeit one that defers handling of the null value to the code, could be:
This does not produce exactly the same SQL, but does provide the same logical result. Translating "complex" SQL queries to LINQ is not always straightforward.
Just youse the above code and this should work fine!
This isn't a full answer for you, but on the left join piece you can use the DefaultIfEmpty operator like so:
I haven't had the need to do any groupby commands yet, so I left that out as to not send you down the wrong path. Two other quick things to note. I have been unable to actually join on two parameters although as above there are ways to get around it. Also, the ?? operator works really well in place of the isnull in SQL.
You're going to want to use the
join into
construct to create a group query.