簡體   English   中英

用linq連接到ef的4個表

[英]join 4 tables with linq to ef

我有四個表/類

public Class Brands
{
  public int Id {get;set;}
  public string Brand {get;set;}
  public String BrandType {get;set;}
}

public Class ManufactureA
{
  public int Id {get;set;}
  public int BrandsId {get;set;}
  public string Product {get;set;} 
  public int Distributor {get;set;}
}

public Class ManufactureB
{
  public int Id {get;set;}
  public int BrandsId {get;set;}
  public string Product {get;set;}
  public int Distributor {get;set;}
}

public Class ManufactureC
{
  public int Id {get;set}
  public int BrandsId {get;set;}
  public string Product {get;set;}
  public int Distributor {get;set;}
}

public Class ManufactureD
{
  public int Id {get;set;}
  public int BrandsId {get;set;}
  public string product {get;set;}
  public int Distributor {get;set;}
}

我正在嘗試制作一張表格,以顯示品牌及其相關制造商的信息。 例如:

品牌1:

產品A,分銷商A

品牌2:

產品B,分銷商B

品牌3:

產品C,分銷商C

品牌4:

產品D,分銷商D

因此,我從這段代碼開始,但是在決定如何實際分組或投影時感到困惑:

var allBrandsManufactures = from brand in Brands
                            join factoryA in ManufactureA on factoryA.BrandsId equals brand.Id
                            join factoryB in ManufactureB on factoryB.BrandsId equals brand.Id
                            join factoryC in ManufactureC on factoryC.BrandsId equals brand.Id
                            join factoryD in ManufactureD on factoryD.BrandsId equals brand.Id

首先,如果可能的話,您應該真正考慮重做數據庫設計。 您的設計當前在表名稱中具有信息。 您應該能夠將所有Manufacture表組合成一個表,該表應該稱為Products。 然后應該有一個額外的列,以指示它是哪個制造商。 像這樣。

public class Products
{
  public int Id {get;set;}
  public int BrandsId {get;set;}
  public string ProductName {get;set;} 
  public int Distributor {get;set;}
  public string Manufacturer {get;set;}
}

或者,您可以創建一個單獨的Manufacturer表並通過外鍵將Product表鏈接到該表,但這僅在您具有要放入數據庫中的其他Manufacturer數據時才真正需要。 然后,如果您有了新的制造商,則不必創建新表。 這也使您的查詢更加容易。

現在,如果您堅持使用這種設計,那么您將需要執行聯合而不是聯接。 最好的方法是將每個制造商查詢分開進行,然后使用Concat進行組合。

var brandA = from brand in Brands
         join factoryA in ManufactureA on brand.Id equals factoryA.BrandsId
         select new { 
            Brand = brand.Brand, 
            Product = factoryA.Product, 
            Distributor = factoryA.Distributor, 
            Manufacturer = "A"};

var brandB = from brand in Brands
         join factoryB in ManufactureA on brand.Id equals factoryB.BrandsId
         select new { 
            Brand = brand.Brand, 
            Product = factoryB.Product, 
            Distributor = factoryB.Distributor, 
            Manufacturer = "B"};

var brandC = from brand in Brands
         join factoryC in ManufactureA on brand.Id equals factoryC.BrandsId
         select new { 
            Brand = brand.Brand, 
            Product = factoryC.Product, 
            Distributor = factoryC.Distributor, 
            Manufacturer = "C"};

var brandD = from brand in Brands
         join factoryD in ManufactureA on brand.Id equals factoryD.BrandsId
         select new { 
            Brand = brand.Brand, 
            Product = factoryD.Product, 
            Distributor = factoryD.Distributor, 
            Manufacturer = "D"};

var result = brandA.Concat(brandB).Concat(brandC).Concat(brandD);

您當然可以選擇任何內容,但是必須在每個查詢中選擇相同的內容,並為屬性使用相同的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM