Help with sql to linq conversion
Could somebody assist me in converting a sql query into LINQ, i'm pretty handy with linq but this is a bit much and i can't download linqpad here!
select
t.*,
开发者_JAVA百科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
By the way I understand your query; you need something like this...
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 is composable so you can break your query into manageable parts. Breaking your query into smaller parts will make it much easier to understand. You can also return complex objects instead of creating flat record rendering from your database.
精彩评论