我需要一些帮助来编写这个linq查询

时间:2009-08-27 11:33:35

标签: c# vb.net linq-to-sql

我有这个SQL查询

select case when AllowanceId is null then 2  else AllowanceId end as AllowanceId
     , d.descen as domdescen
     , t.descen as typdescen
     , case when qty is null then 0 else qty end as qty
     , u.descen as unidescen
from (select t.allowancetypeid, d.allowancedomainid 
      from allowancedomain as d, allowancetype as t
      where t.allowancetypeid in (1,2) and d.active = 1 and t.active = 1) as a left join 
    allowanceqty as q on a.allowancetypeid = q.allowancetypeid and
                         a.allowancedomainid = q.allowancedomainid and 
                         q.allowanceid = 2 inner  join 
    allowancedomain as d on a.allowancedomainid = d.allowancedomainid
    inner  join 
    allowancetype as t on a.allowancetypeid = t.allowancetypeid
    inner join
    unit as u on case when q.unitid is null then 1 else q.unitid end = u.unitid

该查询中有一个变量,它在select子句(第一行)中的数字2和from子句中的“= 2”(在中间)

我写了我想要的东西,请看下面的答案

2 个答案:

答案 0 :(得分:0)

经过长时间的尝试/重试/尝试/重试这里是我想要的......

from a in (from d in AllowanceDomains _
        from t in AllowanceTypes _
        where (new integer(){1,2}).contains(t.AllowanceTypeID) and t.active = true and d.active=true _
        select  t.allowancetypeid,tdescen =t.descen, d.allowancedomainid,ddescen=d.descen)  _
group join qqq in AllowanceQties on new with {.k1 = a.allowancetypeid, .k2 = a.allowancedomainid, .k3 = 2} equals _
                            new with {.k1 = qqq.allowancetypeid, .k2 = qqq.allowancedomainid, .k3 = qqq.allowanceid} into qq = group _
from q in qq.DefaultIfEmpty _
join u in units on if(object.equals(q.unitid,nothing),1,q.unitid) equals u.unitid _
select  AllowanceID =if(object.equals(q.AllowanceID,nothing),2,q.AllowanceID) ,a.tdescen,a.ddescen,qty = if(object.equals(q.qty,nothing),0,q.qty),u.descen

答案 1 :(得分:0)

Linqer是你的朋友。 http://www.sqltolinq.com。它允许您将几乎所有的sql转换为linq,并与您的数据库和dbml一起使用。到目前为止,我从未失败过。它不是免费产品,但物有所值。 [我与Linqer没有任何关联]