0

我有以下代码:

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<> 隐式转换为 。存在显式转换(您是否缺少演员表?)

请帮我

4

1 回答 1

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 命名约定,并且在返回多张发票方面更有意义,而不仅仅是一张。还要注意如何将查询分散到多行使其易于阅读。)

于 2013-03-03T17:11:55.130 回答