Reputation: 13
I am new to C# LINQ, and I am not sure how to write the following query. I have the following table in the database.
ProductManufacturer
- ProductID
- Name
- ManufacturerID
- ManufacturerID
- Name
Each product has a Manufacturer. The requirement is that I need to display a report that will show all the manufacturers (as columns) and show the products for each manufacturer.
Below is an example of what I am trying to achieve http://s24.postimg.org/9baxp7xw5/Capture.png)
Since this is different from how the data is stored in the table, I am unsure of how to retrieve it. Any help would be appreciated. Thanks
Upvotes: 0
Views: 138
Reputation: 19646
Something like this may work (making some assumptions):
var pivot = Manufacturers.Select(m => new
{
Name = m.Name,
Products = Products
.Where(p => p.ManufacturerId == m.ManufacturerId)
.Select(p => p.Name)
.ToList()
});
Upvotes: 1