I’m working with two database tables called Product and Brand. I want to join these tables using LINQ to SQL and map the results to a custom strongly typed class like ProductWithBrandInfo. This way I can pass it properly to my MVC view.
Right now I have separate Product and Brand entity classes. Should I create an additional class for the joined data? I feel like I might be missing something obvious here. Any guidance would be appreciated.
from item in productTable
join brand in brandTable on item.BrandId equals brand.BrandId
where (brand.BrandId == selectedBrandId.Value)
select new
{
item.ProductId,
item.Title,
item.Details,
item.Cost,
BrandTitle = brand.Title
}
<Database Name="Store" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
<Table Name="Brand" Member="Store.Models.Brand">
<Type Name="Store.Models.Brand">
<Column Name="BrandId" Member="BrandId" IsDbGenerated="true" IsPrimaryKey="true" />
<Column Name="Title" Member="Title" />
<Column Name="Details" Member="Details" />
</Type>
</Table>
<Table Name="Product" Member="Store.Models.Product">
<Type Name="Store.Models.Product">
<Column Name="ProductId" Member="ProductId" IsDbGenerated="true" IsPrimaryKey="true" />
<Column Name="Title" Member="Title" />
<Column Name="Details" Member="Details" />
<Column Name="ImagePath" Member="ImagePath" />
<Column Name="Cost" Member="Cost" />
<Column Name="BrandId" Member="BrandId" />
<Association Name="FK_Product_Brand" Member="Brand" ThisKey="BrandId" OtherKey="BrandId" IsForeignKey="true" />
</Type>
</Table>
</Database>