4


我是 linq 和 EF 的初学者。我有 4 张桌子

货币单位

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

要求

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

成本类型

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

请求成本

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

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

结果(一张表)

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

即,如果请求没有成本,则显示该请求的所有成本并设置 Amount 值 0 ,例如 A 表有 4 行,但 B 表在表 A 中有 3 行,请求 2 成本 1 设置金额 0 但在表 B 中未设置,我要设置。

4

4 回答 4

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 中的工作方式 在此处输入图像描述

于 2012-05-28T07:56:16.637 回答
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                     
       }
于 2012-05-28T08:37:22.447 回答
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
                               };
于 2012-06-14T07:29:55.757 回答
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();
于 2015-12-15T13:10:31.560 回答