简体   繁体   English

用linq连接到ef的4个表

[英]join 4 tables with linq to ef

I have four tables/classes 我有四个表/类

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

I am trying to produce a table that will show the brands and the info from their associated manufactures. 我正在尝试制作一张表格,以显示品牌及其相关制造商的信息。 For example: 例如:

Brand1: 品牌1:

ProductA, DistributorA 产品A,分销商A

Brand2: 品牌2:

ProductB, DistibutorB 产品B,分销商B

Brand3: 品牌3:

ProductC, DistributorC 产品C,分销商C

Brand4: 品牌4:

ProductD, DistributorD 产品D,分销商D

So I started with this code but got confused at the point of deciding how to actually group or project it: 因此,我从这段代码开始,但是在决定如何实际分组或投影时感到困惑:

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

First off, if possible, you should really consider redoing your database design. 首先,如果可能的话,您应该真正考虑重做数据库设计。 Your design currently has information in the table names. 您的设计当前在表名称中具有信息。 You should be able to combine all the Manufacture tables into one that probably should be called Products. 您应该能够将所有Manufacture表组合成一个表,该表应该称为Products。 Then there should be an additional column to indicate which manufacturer it is. 然后应该有一个额外的列,以指示它是哪个制造商。 Like this. 像这样。

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

Or you can create a separate Manufacturer table and link the Product table to it via a Foreign Key, but that's only really needed if you have addtional Manufacturer data you want to put in the DB. 或者,您可以创建一个单独的Manufacturer表并通过外键将Product表链接到该表,但这仅在您具有要放入数据库中的其他Manufacturer数据时才真正需要。 Then if you get a new Manufacturer you don't have to create a new table. 然后,如果您有了新的制造商,则不必创建新表。 It also makes your query much easier. 这也使您的查询更加容易。

Now if you are stuck with this design then you'll want to do unions instead of joins. 现在,如果您坚持使用这种设计,那么您将需要执行联合而不是联接。 The best way is to do each Manufacturer query separate and then use Concat to combine them. 最好的方法是将每个制造商查询分开进行,然后使用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);

You can of course select whatever you want, but you have to select the same thing in each query and use the same names for the Properties. 您当然可以选择任何内容,但是必须在每个查询中选择相同的内容,并为属性使用相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM