这篇文章可能有很多重复项。但是我尝试了其中的大多数,不幸的是我的错误仍然发生。
错误为:错误1无法将类型隐式转换
'System.Collections.Generic.List<Report.Business.ViewModels.InvoiceMaster>'
为'System.Collections.Generic.IList<ICSNew.Data.InvoiceHD>'
。存在显式转换(您是否缺少演员表?)
public IList<InvoiceHD> GetAllInvoiceMasterDetailsByInvoiceId(int InvoiceId)
{
var dbMstDtl = ireportrepository.GetAllInvoiceMasterDetailsByInvoiceId(InvoiceId);
var MstDtl = from mst in dbMstDtl
select new Report.Business.ViewModels.InvoiceMaster
{
ModifiedDate = mst.ModifiedDate,
SubTotal = Convert.ToDecimal(mst.SubTotal),
TotalDiscount = Convert.ToDecimal(mst.TotalDiscount),
VAT = Convert.ToDecimal(mst.VAT),
NBT = Convert.ToDecimal(mst.NBT),
AmtAfterDiscount = Convert.ToDecimal(mst.AmtAfterDiscount)
};
return MstDtl.ToList();
}
在某些帖子中,我看到当他们使用return MstDtl.AsEnumerable()。ToList();时,此问题已解决。
但在我的情况下,它也无法正常工作(出现错误)
假设InvoiceMaster
派生自或实现InvoiceHD
,并且您使用的是C#4和.NET 4或更高版本,则可以使用通用方差:
return MstDtl.ToList<InvoiceHD>();
它使用的事实,一个IEnumerable<InvoiceMaster>
是IEnumerable<InvoiceHD>
因为IEnumerable<T>
是协变在T
。
解决该问题的另一种方法是更改MstDtl
使用显式类型的声明:
IEnumerable<InvoiceMaster> MstDtl = ...;
(我还建议遵循常规的C#命名,其中局部变量以小写字母开头,但这是另一回事。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句