如何在linq中加入4个表

时间:2012-05-28 07:46:11

标签: c# linq entity-framework entity-framework-4


我是linq和EF的首发。我有4张桌子

CurrencyUnit

CurrencyUnitID    CurrencyName
-------------------------------
1                    Dolar
2                    Pond

请求

RequestID    WayBillNo
--------------------------
1            10000
2            10001

COSTTYPE

CostTypeID      CostName
-------------------------------
1                 A
2                 B

RequestCost

RequestId    CostId     CurrencyUnitId     Amount   Remark
-----------------------------------------------------------
1              1              1            200
1              2              1            400
2              2              2            1000

我想加入这4个表并获得此结果

结果(A表)

RequestId  WayBillNo  CostId    CostName  CurrencyUnitId   CurrencyUnitName  Amount   Remark
------------------------------------------------------------------------------------------
1           10000        1        A          1                 Dollar       200       
1          10000        2         B          2                 Pond         400
2          10001        1         A          0                               0
2          10001        2         B          2                Pond          1000

我写这个查询

  var items = (from rc in context.CLEARANCE_COST
                join c in context.COST_TYPES on rc.COST_ID equals c.COST_ID 
                join d in context.CLEARANCE_REQUEST on rc.REQUEST_ID equals d.REQUEST_ID
                join f in context.CURRENCY_UNITS on rc.CURRENCY_ID equals f.CURRENCY_ID 
                select new
                           {

                               rc.COST_AMOUNT,
                               c.COST_ID,
                               c.COST_NAME,
                               d.WAYBILL_NO,
                               f.CURRENCY_NAME
                           }).ToList();

但得到这个结果(B表)

RequestId WayBillNo CostId CostName CurrencyUnitId CurrencyUnitName Amount Remark


1           10000        1        A          1                 Dollar       200       
1          10000        2         B          2                 Pond         400
2          10001        2         B          2                Pond          1000

即 如果请求没有成本显示此请求的所有成本并设置金额值0,例如一个表有4行但是B表在表A中有3行用于请求2成本1设置金额0但在表B中没有设置,i想要设置。

4 个答案:

答案 0 :(得分:6)

修改

在linq中尝试左外连接,如下所示....

var query=
    from rc in RequestCost
    join c in CostType on rc.CostId    = c.CostTypeID into RCC
    from rcc in RCC.DefaultIfEmpty() 
    join r in Request on  rc.RequestID = r.RequestID  into RCR
    from rcr in RCR.DefaultIfEmpty()   
    join cu in CurrencyUnit 
    on rc.CurrencyUnitId  = cu.CurrencyUnitID into RCCU
    from rccu in RCCU..DefaultIfEmpty() 

    select new 
       { 
          RequestId = rc.RequestId,
          WayBillNo = rcr != null ? rcr.WayBillNo : "<No WayBillNo>",
          CostId = rc.CostId,
          CostName = rcc != null ? rcc.CostName : "<No CostName>",
          CurrencyUnitID= rc.CurrencyUnitID,
          CurrencyName = rccu != null ? rccu.CurrencyName: "<No CurrencyName>",
          Amount = rc.Amount != null ? rc.Amount : 0 ,
          Remark = rc.Remark != null ? rc.Remark : "<No Remark>",
       }

所以根据你的表格的查询将是

var query=
    from rc in RequestCost
    join c in CostType on rc.CostId    = c.CostTypeID
    join r in Request on  rc.RequestID = r.RequestID    
    join cu in CurrencyUnit on rc.CurrencyUnitId  = cu.CurrencyUnitID 

    select new 
       { 
          rc.RequestId,
          r.WayBillNo,
          rc.CostId,
          c.CostName 
          rc.CurrencyUnitID,
          cu.CurrencyName,
          rc.Amount,
          rc.Remark                     
       }

查看帖子了解更多详情:SQL to LINQ ( Visual Representation )

图像显示了连接在linq中的工作方式 enter image description here

答案 1 :(得分:1)

如果是左外连接(假设RequestCost中的FK可以为空)

var query=
    from rc in RequestCost
    join c in CostType on rc.CostId equals c.CostTypeID into ctype
    join r in Request on  rc.RequestID equals r.RequestID into req
    join cu in CurrencyUnit on rc.CurrencyUnitId equals cu.CurrencyUnitID into unit

    select new 
       { 
          rc.RequestId,
          req.FirstOfDefault() != null ? req.FirstOfDefault().WayBillNo : null,
          rc.CostId,
          ctype.FirstOfDefault() != null ? ctype.FirstOfDefault().CostName : null,
          rc.CurrencyUnitID,
          ctype.FirstOfDefault() != null ? ctype.FirstOfDefault().CurrencyName : null,
          rc.Amount,
          rc.Remark                     
       }

答案 2 :(得分:0)

你可以写这段代码

 var items = from c in context.COST_TYPES
                    join o in context.CLEARANCE_COST .Where(d=>d.REQUEST_ID==8) on c.COST_ID equals o.COST_ID into outer
                    from o in outer.DefaultIfEmpty()
                    select new
                               {
                                   o.CLEARANCE_REQUEST.WAYBILL_NO,
                                   c.COST_NAME,
                                   o.CURRENCY_UNITS.CURRENCY_NAME,
                                   Amount=o==null?0:o.COST_AMOUNT
                               };

答案 3 :(得分:0)

您也可以编写此代码

public class Request()
{
          public int RequestId    { get; set; }
          public RequestCost RequestCost { get; set; }
}

public class RequestCost ()
{
          public int RequestId    { get; set; }
          public CostType CostType { get; set; }
          public CurrencyUnit CurrencyUnit{ get; set; }
}

  var items = context.dbRequest.include("RequestCost")
                                   .include("RequestCost.CurrencyUnit")
                                  .include("RequestCost.CostType").ToList();