3 ms·
I use both raw SQL and the entity framework on a regular basis. That Linq to entities query gets translated pretty directly to SQL. Each of those includes trans
by astine 8y ago
I use both raw SQL and the entity framework on a regular basis. That Linq to entities query gets translated pretty directly to SQL. Each of those includes translates directly to a left join on whatever column is specified as the key. You would need a group by, but no self joins. Assuming that you are familiar with the database schema and are proficient in SQL, it shouldn't be any slower to write the SQL version than the C# version.
It depends a little on the exact columns you need, but it would look something like this, which is a supper common form for a SQL query:
select
m.id Manager,
count(sh.id) as ShiftsCompleted,
sum(iif(sh.coworker = m.id,1,0) as ShiftsWithManager
from staff s
left join manager m
on m.id = s.manager_id
left join shifts sh
on sh.id = s.shifts_id
group by s.id, m.id
The SQL version has the advantage that it's more intuitive to specify the columns that you need so if your query is running slow because you're pulling too much data (something that's happened to me a bunch,) you can omit unneeded columns pretty easily.