我有以下代码:
private GolfManagementEntities db = new GolfManagementEntities();
public class ThanhToan
{
public ThanhToan()
{ }
public Double Total { get; set; }
public DateTime date { get; set; }
}
public List<ThanhToan> listInvoice(DateTime tuNgay, DateTime denNgay)
{
List<ThanhToan> invoice= from i in db.Invoices
where i.Date >= tuNgay && i.Date <= denNgay
group i by i.Date into In
select new ThanhToan {
date= Convert.ToDateTime(In.Key) ,
Total = Convert.ToDouble(In.Sum(s => s.ExtendedCost))
};
return invoice;
}
我收到了这个错误:
无法隐式转换类型'System.Linq.IQueryable&lt;&gt;到。存在显式转换(您是否缺少演员?)
请帮帮我
答案 0 :(得分:5)
您的查询返回IQueryable<THanhToan>
但您尝试返回List<ThanhToan>
。没有可用的隐式转换(根据错误消息),但您可以使用ToList
扩展方法创建列表:
public List<ThanhToan> ListInvoices(DateTime tuNgay, DateTime denNgay)
{
var query = from i in db.Invoices
where i.Date >= tuNgay && i.Date <= denNgay
group i by i.Date into g
select new ThanhToan {
date = Convert.ToDateTime(g.Key),
Total = Convert.ToDouble(g.Sum(s => s.ExtendedCost))
};
return query.ToList();
}
(我已经更改了您的方法名称以遵循.NET命名约定,并且在返回多个发票方面更有意义,而不仅仅是一个。还要注意如何将查询分散到多个行使很多更容易阅读。)