2012-11-12 116 views
2

我有3個表。LINQ加入查詢(在表之間有可爲空的參考)

例如客戶公司地址

  • 客戶端已經接受公司。

  • 公司已收到2個可空的參考地址(帳單和發貨),因此在某些情況下地址可能不存在。

我需要做連接查詢,但如果當Company.BillingAddressCompany.ShippingAddress等於null,我沒有得到所有數據)。

我想它(但它是錯的查詢):

var res = (from client in context.Clients 
    join clientCompany in context.Companies 
    on client.ClientCompanyId equals clientCompany.Id 

    into clientCompanyJoin 

    from company in clientCompanyJoin 
    join addressBilling in context.Addresses 
    on company.BillingAddressId equals addressBilling.Id 

    join addressShipping in context.Addresses 
    on company.ShippingAddressId equals addressShipping.Id 

    select new 
    { 
     Client = client, 
     Company = company, 
     BillingAddress = ??????? 
     ShippingAddress = ??????? 
    } 
); 

能否請你幫我做一個聯接查詢或解釋如何做到這一點?

謝謝。

+0

加入什麼?返回類型的結構應該是什麼?你可以發佈一些代碼或一些僞代碼嗎? – LightStriker

+0

我加了一個例子.. –

回答

5

嘗試這一塊代碼段:

var res = (from client in context.Clients 
      join clientCompany in context.Companies 
      on client.ClientCompanyId equals clientCompany.Id 
      into clientCompanyJoin 
      from company in clientCompanyJoin 
      join addressBilling in context.Addresses 
      on company.BillingAddressId equals addressBilling.Id 
      where !String.IsNullOrEmpty(addressBilling.Address) 
      join addressShipping in context.Addresses 
      on company.ShippingAddressId equals addressShipping.Id 
      where !String.IsNullOrEmpty(addressShipping.Address) 
      select new 
      { 
       Client = client, 
       Company = company, 
       BillingAddress = addressBilling.Address, 
       ShippingAddress = addressShipping.Address 
      }); 

新增:按你的意見,在這裏是您需要的一段代碼片段。現在你可以有你客戶公司數據,即使ShippingAddressIdBillingAddressId等於null像什麼Left JoinSQL做。

var res = (from client in context.Clients 
      join company in context.Companies 
      on client.ClientCompanyId equals company.Id 
      join addressBilling in context.Addresses 
      on company.BillingAddressId equals addressBilling.Id 
      into addrBillingGroup 
      from gAddrBilling in addrBillingGroup.DefaultIfEmpty() // left join 
      join addressShipping in context.Addresses 
      on company.ShippingAddressId equals addressShipping.Id 
      into addrShippingGroup 
      from gAddrShipping in addrShippingGroup.DefaultIfEmpty() // left join 
      select new 
      { 
       Client = client, 
       Company = company, 
       BillingAddress = 
        gAddrBilling == null ? null : gAddrBilling.Address, 
       ShippingAddress = 
        gAddrShipping == null ? null : gAddrShipping.Address 
      }); 
+0

它不起作用。我不明白一些事情(例如!String.IsNullOrEmpty(addressBilling.Address))和addressBilling不包含Address字段。 –

+0

我認爲這些地方的問題「on company.BillingAddressId等於addressBilling.Id」,因爲在某些情況下BillingAddressId和ShippingAddressId等於NULL –

+0

是否要過濾BillingAddressId和ShippingAddressId等於NULL的記錄?其次,如果BillingAddressId和ShippingAddressId是整數類型,那它怎麼可以是NULL? –

2

我想你想做一個外連接。下面是一個例子,如何做到這一點的客戶和訂單的「羅斯文」數據庫:

var ctx = new NorthwindDataContext(); 
var customers = from c in ctx.Customers 
    join o in ctx.Orders 
    on c.CustomerID equals o.CustomerID into inJoin 
    from outJoin in inJoin.DefaultIfEmpty() 
    orderby c.CustomerID, outJoin.OrderID 
    select new 
    { 
     c.CustomerID, 
     c.CompanyName, 
     OrderID = (int?)outJoin.OrderID, 
     OrderDate = (DateTime?)outJoin.OrderDate 
    };