linq to sql 查找所有开票金额大于回款金额的项目
查找所有开票金额大于回款金额的项目
TB_Projects 项目表
TB_Recipts 发票表
TB_Finances 回款表
TB_Projects 一对多 TB_Recipts
TB_Projects 一对多 TB_Finances
1 select TB_Projects.ProjectCode, (CASE WHEN SUM(TB_Receipt.ReceiptMoney) IS NULL THEN 0 ELSE SUM(TB_Receipt.ReceiptMoney) END)as kp, 2 (CASE WHEN SUM(TB_ProjectFinance.RealityMoney) IS NULL THEN 0 ELSE SUM(TB_ProjectFinance.RealityMoney)END) as hk from TB_Projects left join TB_Receipt 3 on TB_Projects.Id=TB_Receipt.Project_Id left join TB_ProjectFinance on TB_Projects.Id=TB_ProjectFinance.Project_Id where TB_Projects.ProjcetManager=N'李优'
group by TB_Projects.ProjectCode
1 var query = (from p in Projects 2 join q in Recipts on p.Id equals q.Project.Id into l 3 from lp in l.DefaultIfEmpty()//左链接 两个表 生成新表 Projects---Recipts 4 join t in Finances on p.Id equals t.Project.Id into z 5 from zp in z.DefaultIfEmpty() //左链接 两个表 Projects---Finance 6 where p.ProjcetManager.Contains(person) //项目经理是当前登录人 7 select new 8 { 9 ProjectName = p.ProjectName, 10 ProjectCode = p.ProjectCode, 11 KaiPiao = lp.ReceiptMoney == null ? 0 : lp.ReceiptMoney, 12 HuiKuan = zp.RealityMoney == null ? 0 : zp.RealityMoney 13 } 14 into g 15 group g by new { g.ProjectCode,g.ProjectName } into k //根据 编号和姓名 分组 16 select new 17 { 18 ProjectCode = k.Key.ProjectCode, 19 ProjectName=k.Key.ProjectName, 20 KaiPiao = k.Sum(t => t.KaiPiao) == null ? 0 : k.Sum(t => t.KaiPiao), 21 HuiKuan = k.Sum(t => t.HuiKuan) == null ? 0 : k.Sum(t => t.HuiKuan), 22 } into d 23 where d.KaiPiao > d.HuiKuan 24 select new 25 { 26 ProjectName=d.ProjectName, 27 KaiPiao = d.KaiPiao, 28 HuiKuan = d.HuiKuan, 29 ProjectCode = d.ProjectCode 30 } 31 ).ToList(); 32 33 34 return (from q in query 35 select new TB_ProjectsInputDto 36 { 37 ProjectName = q.ProjectName, 38 ProjectCode = q.ProjectCode, 39 ReceiptMoney = q.KaiPiao, 40 ReceivedPayment = q.HuiKuan 41 }).ToList();