【问题标题】:Is it possible to Query a Query?是否可以查询查询?
【发布时间】:2014-01-07 20:27:21
【问题描述】:

我正在努力为我正在创建的订单网站进行多对多分组。

目前我的数据是这样出来的

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

感谢您的帮助,我真的很努力解决这个问题

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    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)
                    });
    

    结果是 Items 的数量,按特定 供应商,按特定 配额,但没有每个供应商的总数。您可以轻松地从中进行数学运算。

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

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

    【讨论】:

    • 谢谢!如何将结果导入模型?
    • 您可以创建一个包含结果行信息的新类。然后你可以修改最后一个选择语句来返回这个类的对象。然后将此结果作为 List 或 IEnumerable 添加到模型中
    • 我不明白你的意思,我对此很陌生,你能告诉我它填补了你制作的课程吗?我的课是一样的
    • 我编辑了第一个代码块。现在你有已知类型的对象列表。你只需要计算总数。
    • 无法将 itapp.models.tblquote 转换为 itapp.model.quote?您需要查看我的表名代码等吗?
    猜你喜欢
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多