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:
ProductA, DistributorA
Brand2:
ProductB, DistibutorB
Brand3:
ProductC, DistributorC
Brand4:
ProductD, DistributorD
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. Then there should be an additional column to indicate which manufacturer it is. Like this.
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. 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
Concatto combine them.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.