Q:

Populating classes with many to many linq data

Here is my c#
var result = db.Quotes_Items_Suppliers
            .Include(tbl => tbl.tblItems)
            .Include(tbl => tbl.tblQuotes)
            .Include(tbl => tbl.tblSuppliers).GroupBy(tbl => new { tbl.tblQuotes, tbl.tblSuppliers, tbl.tblItems })
            .Select(grouped => new QuoteViewModel
            {
                Quote = new Quote { ID = grouped.Key.tblQuotes.ID,
                                    QuoteNo = grouped.Key.tblQuotes.QuoteNo ,
                                    Date = grouped.Key.tblQuotes.RequestDate},

                Supplier = new Supplier { ID = grouped.Key.tblSuppliers.ID,
                                          Name = grouped.Key.tblSuppliers.Supplier,
                                          TelNo = grouped.Key.tblSuppliers.Tel },

                Item = new Item { ID = grouped.Key.tblItems.ID,
                                  Name = grouped.Key.tblItems.Name,
                                  PartNo = grouped.Key.tblItems.PartNo },
                Quantity = grouped.Sum(tbl => tbl.Quantity),
                Price = grouped.Sum(tbl => tbl.Price)
            });

            AllQuotes AllQuotes = new AllQuotes();
            foreach (var quoteId in db.Quotes_Items_Suppliers.Select(qis => qis.QuoteID).Distinct())
            {
                int id = quoteId;
                foreach (var row in result.Where(row => row.Quote.ID == id))
                {
                    Request Req = new Request();
                    Req.Quote = new Quote { ID = row.Quote.ID,
                                    QuoteNo = row.Quote.QuoteNo,
                                    Date = row.Quote.Date };
                    // should this be another foreach loop to select all the suppliers for this quote?
                    Req.Suppliers = new Supplier { ID = row.Supplier.ID,
                                              Name = row.Supplier.Name,
                                              TelNo = row.Supplier.TelNo };
                    // again should this be another foreach loop to select all the items for this quote?
                    Req.Items = new Item { ID = row.Item.ID,
                                      Name = row.Item.Name,
                                      PartNo = row.Item.PartNo };
                    Req.Quantity = row.Quantity;
                    Req.Price = row.Price;
                    AllQuotes.Reqs.Add(Req);
                }
            }

here is my class:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ITAPP.Models
{
    public class Quote
    {
        public int ID { get; set; }
        public string QuoteNo { get; set; }
        public DateTime? Date { get; set; }
    }
    public class Item
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string PartNo { get; set; }
    }
    public class Supplier
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string TelNo { get; set; }
    }
    public class QuoteViewModel
    {
        public Quote Quote { get; set; }
        public Item Item { get; set; }
        public Supplier Supplier { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
    }

    public class Request
    {
        public Quote Quote { get; set; }
        public Item Items { get; set; }
        public Supplier Suppliers { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }
    }
    public class AllQuotes
    {
        public List<Request> Reqs { get; set; }
    }
}

im not sure im getting this right, infact i know im not.
I think i need a list for suppliers and a list for items in each request, then i do a foreach for each quote items and each quote suppliers to add the data to those lists? is that right?
basically i should have:-
Quote
x ammount of Quote items
x ammount of suppliers that quoted for each item and their prices
Hopefully the above is enough for people to understand what im trying to do?
Thanks
EDIT:Samples
Here is what i want the data to look like
IS0001 20/11/2013

           | HP DL360p  | Samsung 840 Pro 256GB | Total 
ITSupplier | 1132       | 477                   | 1609
ITComms    | 1160       | 510                   | 1670
ITSol      | 1135       | 160                   | 1295

Here is the table layout
tblQuotes
ID | QuoteNo | Date

tblItems
ID | Itemname | Part No

tblSuppliers
ID | SupplierName | TelNo

tblQuotes_Items_Suppliers
ID | QuoteID | ItemID | SupplierID | Quantity | Price

so each quote should show each suppliers prices for each item

A:

I don't think you need to do any grouping. If you need to break down everything in a table, then you need all of that granular data in your viewmodel. To add up the totals, just do this in your view.
So create your viewmodel (note: I'm not using your QuoteViewModel class at all. I'm using the AllQuotes class as your viewmodel) like this:
var viewmodel = new AllQuotes();

viewmodel.Reqs = db.Quotes_Items_Suppliers
        .Include(tbl => tbl.tblItems)
        .Include(tbl => tbl.tblQuotes)
        .Include(tbl => tbl.tblSuppliers)
        .Select(tbl => new Request
        {
            Quote = new Quote { ID = tbl.tblQuotes.ID,
                                QuoteNo = tbl.tblQuotes.QuoteNo ,
                                Date = tbl.tblQuotes.RequestDate},

            Supplier = new Supplier { ID = tbl.tblSuppliers.ID,
                                      Name = tbl.tblSuppliers.Supplier,
                                      TelNo = tbl.tblSuppliers.Tel },

            Item = new Item { ID = tbl.tblItems.ID,
                              Name = tbl.tblItems.Name,
                              PartNo = tbl.tblItems.PartNo },
            Quantity = tbl.Quantity,
            Price = tbl.Price
        }).ToList();

Basically it's just a copy of your db table.
Then in your view, just create a table and do all the necessary grouping there. This creates a table per quote
E.g.
@foreach (var quote in Model.Reqs.GroupBy(r => r.Quote.ID).OrderBy(g => g.First().Quote.QuoteNo))
{
    <h3>@quote.First().Quote.QuoteNo @quote.First().Quote.Date</h3>
    <table>
        <thead>
            <tr>
                <th><!--blank top-left cell--></th>
                @foreach (var item in quote.GroupBy(r => r.Item.ID).OrderBy(g => g.First().Item.Name))
                {
                    <th>@item.First().Item.Name</th>
                }
                <th>Total Price</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var supp in quote.GroupBy(r => r.Supplier.ID))
            {
                <tr>
                    <td>@supp.First().Supplier.Name</td>                            
                    @foreach (var item in supp.OrderBy(r => r.Item.Name))
                    {
                        <td>@item.Price</td>
                    }
                    <td>@supp.Sum(r => r.Price)</td>
                </tr>
            }
        </tbody>
    </table>
}

Please accept that this is all off the top of my head as I don't have an IDE or your db to hand, so there may be syntax errors. Hopefully it will get you going in the right direction.
edit
Also, note that I've made the assumption that all suppliers will have quoted for all items.

