0

我有四个表/类

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
4

1 回答 1

0

首先,如果可能,您应该真正考虑重做数据库设计。您的设计当前在表名中有信息。您应该能够将所有 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;}
}

或者您可以创建一个单独的制造商表并通过外键将产品表链接到它,但这只有在您想要放入数据库中的其他制造商数据时才真正需要。然后,如果您获得新的制造商,则不必创建新表。它还使您的查询更容易。

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

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

于 2012-12-21T04:04:31.130 回答