2012-07-11 116 views
0

如何将LINNER to SQL中的INNER JOIN和LEFT JOIN转换为以下SQL查询?谢谢!INNER JOIN左加入LINQ to SQL

SELECT transactions.postdate, 
     transactions.clientkey AS TransClientKey, 
     transactions.type   AS TransType, 
     clients.clientno, 
     Isnull(clients.nostmt, 0) AS CliNoStmt, 
     Isnull(aging.nostmt, 0) AS AgeNoStmt, 
     pmtchecks.* 
FROM ((pmtchecks 
     INNER JOIN transactions 
       ON pmtchecks.transkey = transactions.transkey) 
     INNER JOIN clients 
       ON transactions.clientkey = clients.clientkey) 
     LEFT JOIN aging 
       ON (transactions.clientkey = aging.clientkey) 
       AND (pmtchecks.debtorkey = aging.debtorkey) 
WHERE (pmtchecks.debtorkey = 36927) 
     AND (transactions.status = 0) 
     AND (transactions.postdate <= '31-May-2012') 
     AND ((transactions.postdate >= '01-May-2012') 
       OR (clients.clientno = 'UNKNOWN')) 
ORDER BY pmtchecks.checkdate, 
      pmtchecks.checkno 

回答

1

你好,这是一种伪代码,我CNT说,它完全正确的,但这个想法将是完全一样得到结果

var anonymousType= (from pm in pmtchecks 
     join tr in transactions 
     on pm.transkey equals tr.transkey //INNERJOIN 
     join cl in clients 
     on tr.clientKey equals cl.clientKey 
     join ag in aging 
     on pm.debtorkey equals ag.debtorKey into ljoin //Left Join 
     from lj in ljoin.DefaultOrEmpty() 
     where pm.debortkey==36927 && tr.status==0 && tr.postdate<="31-May-2012" && tr.postdate>="01-May-2012" //u will have to change this to date format first 
     Select new {PostDate=tr.postdate, TransClientKey=tr.clientkey,TransType=tr.type,ClientNo=cl.clientno,CliNoStmt=cl.nomst ?? 0,AgeNoStmt=ag.nomst ??0,Pmtchecks=pm } //Anonymous type from this you can extract the values and fill to your custom type 
     ).OrderBy(o=>o.Pmtchecks.checkdate).OrderBy(o=>o.Pmtchecks.checkno).ToList(); 

希望这会有所帮助。

+0

其中pm.debortkey == 36927 && tr.status == 0 && tr.postdate <= '31可能-2012' && tr.postdate> = 01-May-2012'你如何改变日期时间格式? – 2012-07-12 21:28:13

+0

PostDate字段是事务表中的日期时间数据类型。 – 2012-07-12 21:32:44

+0

DateTime dt = new DateTime(2012,4,31); tr.postdate <= dt; – ethicallogics 2012-07-13 03:55:47

1

LINQ Query Samples

EDITED

var pmtchecks = from p in urcontext.pmtchecks 
         join t in urcontext.transactions on p.transkey equals t.transkey 
         join a in urcontext.aging on t.clientkey equals a.clientkey into details 
         from d in details.Where(a => (a.debtorkey == p.debtorkey)).DefaultIfEmpty() 
         where (p.debtorkey == 36927 && t.status == 0 && t.postdate <= '31-May-2012' 
         && (t.postdate >= '01-May-2012' || c.clientno == 'UNKNOWN')) 
         orderby p.checkdate, p.checkno 
         select new 
         { 
          t.postdate, 
          t.clientkey, 
          // TransClientKey = t.clientkey, //only works if TransClientKey is property 
          t.type ,  
          //TransTypet = t.type ,//property 
          c.clientno, 
          c.nostmt, 
          //CliNoStmt = c.nostmt ?? 0,//property 
          a.nostmt//, 
          //AgeNoStmt = nostmt ?? 0,//property 
          //p. ... //follow above for p columns 
         }; 
+0

是否有原因:'.DefaultIfEmpty()'在下一行? – Trisped 2012-07-11 17:47:22

+0

@Trisped - 将其突出显示为“LEFT”中的差异。 – user1166147 2012-07-11 17:50:34

+0

a =>(t.ClientKey == a.ClientKey && p.DebtorKey == a.DebtorKey – 2012-07-17 17:02:11