是否可以查询查询?

时间:2013-12-19 10:15:27

标签: c# asp.net-mvc linq razor entity-framework-4

我正在为一个订单网站创建很多对很多人进行分组。

目前我的数据就是这个

IS0001; 20/11/2013 00:00:00; 

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

我想要的是这个

IS0001 20/11/2013

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

继承我当前的查询

var tblQuoting = db.Quotes_Items_Suppliers.Select(qis => qis.QuoteID).Distinct();
var model = new List<QuoteViewModel>();
foreach (var Quotes in tblQuoting)
{
    var ModelItem = new QuoteViewModel
    {
        Quote = db.Quotes.Where(q => q.ID == Quotes).ToList(),
        Items = db.Quotes_Items_Suppliers.Include(t => t.tblItems).Include(t => t.tblSuppliers).Where(i => i.QuoteID == Quotes).ToList()
    };
    model.Add(ModelItem);
}

要获得结果,我需要对供应商和物品进行分组。我认为,如果我可以查询查询,我可以对项目ID进行分组,然后对供应商ID进行分组。但我不确定是否或如何开始查询查询,是否可能?

我的表格结构如下: -

tblQuotes
ID | QuoteNo | Date

tblItems
ID | Itemname | Part No

tblSuppliers
ID | SupplierName | TelNo

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

感谢您的帮助,我真的很难与这个

1 个答案:

答案 0 :(得分:2)

试试这个:

var result = dbQuoteItemsSuppliers
            .Include(tbl => tbl.Item)
            .Include(tbl => tbl.Quote)
            .Include(tbl => tbl.Supplier).GroupBy(tbl => new { tbl.Quote, tbl.Supplier, tbl.Item })
            .Select(grouped => new QuoteItemSuppliers
                {
                    Quote = grouped.Key.Quote,
                    Supplier = grouped.Key.Supplier,
                    Item = grouped.Key.Item,
                    Quantity = grouped.Sum(tbl => tbl.Quantity),
                    Price = grouped.Sum(tbl => tbl.Price)
                });

结果是 商品 的数量,具体 供应商 ,具体 配额 ,但每个供应商没有总计。你可以从中轻松做数学。

枚举每个不同配额的槽行的示例:

foreach (var quoteId in dbQuotes.Select(quote => quote.Id).Distinct())
        {
            int id = quoteId;
            foreach (var row in result.Where(row => row.Quote.Id == id))
            {
                // here you got only rows for specific Quota
            }
        }

哦,这是我正在研究的模型:

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 QuoteItemSuppliers
{
    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; }
}