本文介绍了无法将类型'System.Linq.IQueryable<>隐式转换为.存在显式转换(您是否缺少演员表?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

请帮助我

推荐答案

您的查询返回了IQueryable<THanhToan>,但是您试图返回的是List<ThanhToan>.没有可用的隐式转换(根据错误消息),但是您可以使用 扩展方法来创建列表:

Your query returns IQueryable<THanhToan> but you're trying to return a List<ThanhToan>. There's no implicit conversion available (as per the error message) but you can use the ToList extension method to create a list:

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命名约定,并且在返回多个发票而不只是一张发票时更有意义).还要注意,将查询分散到多行很多更容易阅读.)

(I've changed your method name to follow .NET naming conventions and make more sense in terms of it returning multiple invoices, not just one. Also note how spreading the query over multiple lines makes it much easier to read.)

这篇关于无法将类型'System.Linq.IQueryable&lt;&gt;隐式转换为.存在显式转换(您是否缺少演员表?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:28