带有左连接的sql到linq语句,group by和case语句

时间:2013-02-13 02:47:57

标签: c# sql asp.net-mvc-3 linq-to-sql

我是MVC3的新手,有一点问题。我想将此SQL语句转换为Linq。任何人都可以帮我解决这个问题,这是我的sql声明:

SELECT a.payment_ref_no, 
       c.institution_name, 
       a.check_date, 
       batchstatus = CASE 
                       WHEN d.mccount = Count(b.check_detail_no) THEN 
                       'Completed' 
                       WHEN d.mccount IS NULL THEN 'Approved' 
                       WHEN d.mccount < Count(b.check_detail_no) THEN 
                       'Partially Processed' 
                     END, 
       noofpayments=Count(b.check_detail_no), 
       totalamount=Sum(b.check_amount), 
       d.mccount 
FROM   check_request a 
       JOIN check_details b 
         ON a.request_ref_no = b.request_ref_no 
       JOIN institution c 
         ON a.company_code = c.company_code 
       LEFT JOIN vw_batchstatus d 
              ON a.request_ref_no = d.request_ref_no 
WHERE  a.payment_ref_no IS NOT NULL 
GROUP  BY a.payment_ref_no, 
          a.check_date, 
          c.institution_name, 
          d.mccount 

2 个答案:

答案 0 :(得分:0)

主要是从记忆中完成,可能是一些问题,但应该是朝着正确的方向迈出的一步。

var test =  from a in check_request
        join b in check_details on a.request_ref_no equals b.request_ref_no
        join c in institution on a.company_code equals c.company_code
        join d in vw_batchstatus on a.request_ref_no equals d.request_ref_no into dvwinner
        from d in dvwinner.DefaultIfEmpty()
        where a.payment_ref.HasValue
        group a by new (a.payment_ref_no, a.check_date, c.institution_name, d.mccount) into gr1
        select new {
            ref_no = a.payment_ref_no,
            inst_name = c.institution_name,
            check_date = a.check_date,
            batstat = !d.mccount.HasValue ? 'Approved' : d.mccount == b.check_detail_no.Count() ? 'Completed' : 'Partially Processed',
            noofpayments = b.check_detail_no.Count(),
            ttlamount = gr1.Sum(p=>p.check_amount),
            mccount = d.mccount
        };

答案 1 :(得分:0)

谢谢Kyle的帮助。我终于解决了自己的问题,这里是我的sql语句的linq

var test =  from a in CHECK_REQUESTs
    join b in CHECK_DETAILS on a.REQUEST_REF_NO equals b.REQUEST_REF_NO
    join c in INSTITUTIONs on a.COMPANY_CODE equals c.COMPANY_CODE
    join d in Vw_BatchStatus on a.REQUEST_REF_NO equals d.REQUEST_REF_NO into t from rt in     t.DefaultIfEmpty()
        where a.PAYMENT_REF_NO != string.Empty 
        let institutionName = (string)c.Institution_Name
        let mcCount = (int)rt.Mccount
        group b by new 
        {
        a.PAYMENT_REF_NO, 
        a.Check_Date, 
        institutionName, 
       mcCount
        } into gr1
        select new 
        {
        gr1.Key.PAYMENT_REF_NO, 
        gr1.Key.institutionName, 
        gr1.Key.Check_Date,
        batchstatus = (gr1.Key.mcCount == gr1.Count()) ? "Completed" : 
        (gr1.Key.mcCount < gr1.Count()) ? "Partially Processed": 
        (gr1.Key.mcCount == null ) ? "Approved" : " ",
        noofpayments = gr1.Count(),
        totalamount = gr1.Sum(c => c.Check_Amount)
        };