如果从 Oracle 数据库中的 TOAD 执行,我有这个 SQL 查询运行速度很快,其中 Jobs 表包含超过 200 万条记录。
select * from Technicians A
left join
(select TechnicianCode,count(*)JobCount from Jobs
where TxnCode in ('Jc','Jcd') group by TechnicianCode) B
on B.TechnicianCode =A.TechnicianCode
无法以有效的方式将其转换为 linq。 有人可以帮忙吗?
最佳答案
你可以试试这个
var txnDetails = from job in jobs
where job.TxnCode == "Jc" || job.TxnCode == "Jcd"
group job by job.TechnicianCode into g
select new { TechnicianCode = g.Key, count = g.Count() };
然后你就可以用join做正常的select了
from technician in Technicians
join txn in txnDetails on technician.TechnicianCode equals
txn.TechnicianCode into tg
from t in tg.DefaultIfEmpty()
select new {Count = t==null? 0: t.count, ...}
关于c# - 具有连接和计数的快速 linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404434/