这篇文章有很多可能的重复项。但我尝试了其中的大部分,不幸的是我的错误仍然存在
发生。
错误是:错误 1 无法隐式转换类型'System.Collections.Generic.List<Report.Business.ViewModels.InvoiceMaster>'
to '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();
}
在一些帖子中,我看到他们使用时解决了这个问题返回MstDtl.AsEnumerable().ToList();
但就我而言,它也不起作用(出现错误)
假设InvoiceMaster
源自或实现InvoiceHD
,并且您使用的是 C# 4 和 .NET 4 或更高版本,则可以只使用通用方差:
return MstDtl.ToList<InvoiceHD>();
这利用了以下事实:IEnumerable<InvoiceMaster>
is an IEnumerable<InvoiceHD>
因为IEnumerable<T>
is 协变 in T
.
解决这个问题的另一种方法是更改声明MstDtl
使用显式类型:
IEnumerable<InvoiceMaster> MstDtl = ...;
(我还建议遵循常规 C# 命名,其中局部变量以小写字母开头,但这是另一回事。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)