2013-12-19 169 views
2

即時通訊掙扎與許多到一個訂單網站即時創建分組。是否可以查詢查詢?

目前我的數據出來,因爲這

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 

感謝您的幫助,我真的與這一個

回答

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

謝謝!我如何將結果導入模型? – AlexW

+0

您可以創建一個包含結果行信息的新類。 然後你可以修改最後一條select語句來返回這個類的對象。 然後將此結果添加到模型列表或IEnumerable – mbudnik

+0

我不明白你的意思,我對此很新,你能告訴我它填補了你所做的課程嗎?我的課程是相同的 – AlexW