我有这个SQL查询,如果从Oracle数据库中的TOAD执行,其中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。 有人可以帮忙吗?
答案 0 :(得分:2)
你可以试试这个
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() };
然后你可以用连接
进行正常选择 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, ...}
答案 1 :(得分:1)
要进行左连接,请修改@ SAJ答案,如下所示:
var result = from technician in Technicians
join txn in txnDetails on technician.TechnicianCode equals
txn.TechnicianCode
into j
from lj in j.DefaultIfEmpty()
select lj;
答案 2 :(得分:0)
这是你在找?
var result = from technician in Technicians
join job in Jobs on technician.TechnicianCode equals job.TechnicianCode into A
from job in A.DefaultIfEmpty()
where job.TxnCode == "Jc" || job.TxnCode == "Jcd"
select new
{
Tech = technician,
Job = job
};
要获得计数,您只需使用:
var count = result.Where(r => r.Job != null).Count();
答案 3 :(得分:-1)
尝试代码:
var result= (from p in Technicians
join q in Jobs.where(c=>c.TxnCode=="JC" || c.TxnCode==c."Jcd").GroupBy(c=>c.TechnicianCode)
.Select(m => new { jobcount = m.Count(), TechnicianCode = m.FirstOrDefault().TechnicianCode, TxnCode=m.FirstOrDefault().TxnCode}).ToList()
on p.TechnicianCode equals q.TechnicianCode into jj from kk in jj.DefaultIfEmpty()
select new {
jobcount=kk.jobcount,
TxnCode=kk.TxnCode,
TechnicianCode=p.TechnicianCode
}).ToList();