Anderson Adams
Anderson Adams

Reputation: 13

LINQ C# - Writing a query to display data in a pivot style

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.

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

Answers (1)

Dave Bish
Dave Bish

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

Related Questions