我在这里遇到麻烦,
我的班级如下:
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”
如果我在group.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)
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句