有人可以协助我将SQL查询转换为LINQ,我对linq非常方便,但这有点多了,我不能在这里下载linqpad!
select
t.*,
l.*
from
email_templates t
left join
(select
id as email_id,
sent_at,
sent_by
from
email_log
where
id = (select max(id)
from email_log
where as_at_date = '20100618'
group by template_id )
)l
on t.id = v.template_id
答案 0 :(得分:1)
顺便说一句,我理解你的查询;你需要这样的东西......
var maxId = (from e in email_log
where e.as_at_date < new DateTime(2010, 06, 18)
group e by e.template_id into grouped
select grouped.Max(a => a.id)).First();
var selectedEmailLog = from e in email_log
where e.id == maxId
select new
{
email_id = e.id,
e.sent_at,
e.sent_by,
e.template_id,
};
var seletedRows = from t in email_templates
join l in selectedEmailLog
on t.id equals l.template_id into tls
from tl in tls
select new
{
t.id,
tl.email_id,
tl.sent_at,
tl.sent_by,
};
... LINQ是可组合的,因此您可以将查询分解为可管理的部分。将查询分解为更小的部分将使其更容易理解。您还可以返回复杂对象,而不是从数据库创建平面记录呈现。