2014-01-07 77 views
0

IM有这里有些麻烦,如何将表格转换为子类?

我的等级如下表所示: -

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

我的查询如下: -

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 = grouped.Key.tblQuotes, 
       Supplier = grouped.Key.tblSuppliers, 
       Item = grouped.Key.tblItems, 
       Quantity = grouped.Sum(tbl => tbl.Quantity), 
       Price = grouped.Sum(tbl => tbl.Price) 
      }); 

当我试图把一个表到一个子类(即tblQuotes到Quote)我得到“不能转换ITAPP.Models.tblQuotes ITAPP.Models.Quote”

如果我在grouped.key.tblQuotes后键入一个点我得到该字段s从tblQuotes

即时通讯不知道如何让表进入子类?

感谢所有帮助

回答

1

你可以尝试这样的

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.Date}, 
       Supplier = grouped.Key.tblSuppliers, 
       Item = grouped.Key.tblItems, 
       Quantity = grouped.Sum(tbl => tbl.Quantity), 
       Price = grouped.Sum(tbl => tbl.Price) 
      }); 
+0

由于工作的一种享受! – AlexW