我怎样才能将这个sql语法转移到linq?

时间:2015-02-10 08:51:11

标签: sql linq

SELECT t0.SHOPID, 
       t0.CONSGIPOS,
       t1.StoreName as CStorename,
       t0.ORDPOS,
       t2.StoreName as OStoreName
FROM EC_ORDER t0
LEFT JOIN HIERARCHY t1
  ON (t0.CONSGIPOS = t1.STOREID)
LEFT OUTER JOIN HIERARCHY t2
  ON (t2.STOREID = t0.ORDPOS)

1 个答案:

答案 0 :(得分:0)

DefaultIfEmpty中使用LINQ左连接。试试这个:

var result = (from t0 in context.EC_ORDER
               join t1 in context.HIERARCHY on t0.CONSGIPOS == t1.STOREID into t1tmp
                from t1t in t1tmp.DefaultIfEmpty()
               join t2 in context.HIERARCHY on t0.ORDPOS == t2.STOREID into t2tmp
                from t2t in t2tmp.DefaultIfEmpty()
              select {
                         t0.SHOPID, 
                         t0.CONSGIPOS,
                         CStorename = t1t != null ? t1t.StoreName : null,
                         t0.ORDPOS,
                         OStoreName = t2t != null ? t2t.StoreName : null
                      }).ToList();