用linq连接4个表到ef

时间:2012-12-21 02:42:19

标签: c# linq entity-framework

我有四个表/类

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;}
}

我正在尝试制作一个表格,以显示其相关制造商的品牌和信息。例如:

Brand1:

ProductA,DistributorA

Brand2:

ProductB,DistibutorB

Brand3:

ProductC,DistributorC

Brand4:

ProductD,DistributorD

所以我从这段代码开始,但在决定如何实际分组或投影时感到困惑:

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

1 个答案:

答案 0 :(得分:0)

首先,如果可能,您应该考虑重做数据库设计。您的设计目前在表名中包含信息。您应该能够将所有制表组合成一个可能应该称为产品的制表。然后应该有一个额外的列来指示它是哪个制造商。像这样。

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;}
}

或者您可以创建一个单独的制造商表,并通过外键将Product表链接到它,但只有在您想要添加到DB中的附加制造商数据时才需要这样做。然后,如果您获得新的制造商,则无需创建新表。它还使您的查询更容易。

现在,如果您坚持使用此设计,那么您将需要进行工会而不是连接。最好的方法是将每个制造商的查询分开,然后使用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);

您当然可以选择任何您想要的内容,但您必须在每个查询中选择相同的内容并为属性使用相同的名称。