C# 如何将表放入子类中?

C# 如何将表放入子类中?,c#,linq,entity-framework,class,C#,Linq,Entity Framework,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; } publi

我这里有些麻烦

我的班级如下:-

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)
            }); 
我的质询如下:-

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后键入一个点,我将从tblQuotes获取字段

我不知道如何将表放入子类


谢谢你的帮助

你可以这样试试

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