【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'无法将类型“System.Collections.Generic.List< >”隐式转换为“System.Collections.Generic.IList< >”
【发布时间】:2014-10-22 06:14:18
【问题描述】:

这篇文章有很多可能的重复项。但我尝试了其中的大多数,不幸的是我的错误仍然存​​在 发生。

错误是:错误 1 ​​无法将类型 'System.Collections.Generic.List&lt;Report.Business.ViewModels.InvoiceMaster&gt;' 隐式转换为 'System.Collections.Generic.IList&lt;ICSNew.Data.InvoiceHD&gt;'。存在显式转换(您是否缺少演员表?)

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();

解决了这个问题

但在我的情况下它也不起作用(出现错误)

【问题讨论】:

  • 你试过return MstDtl as IList&lt;InvoiceHD&gt;
  • 您正在创建InvoiceMaster 的集合,因此您的签名需要为public IList&lt;InvoiceMaster&gt; ...
  • InvoiceMaster 和 InvoiceHD 有什么关系?
  • @vallabha:每次都只会返回null,因为像这样的LINQ查询的结果从不IList&lt;T&gt;,不管T .
  • @JonSkeet 感谢更新我

标签: c# linq


【解决方案1】:

假设 InvoiceMaster 派生自或实现 InvoiceHD,并且您使用的是 C# 4 和 .NET 4 或更高版本,您可以只使用泛型变量:

return MstDtl.ToList<InvoiceHD>();

这使用了IEnumerable&lt;InvoiceMaster&gt;IEnumerable&lt;InvoiceHD&gt; 的事实,因为IEnumerable&lt;T&gt;T 中是协变的

另一种解决方法是更改​​MstDtl 的声明以使用显式类型:

IEnumerable<InvoiceMaster> MstDtl = ...;

(我还建议遵循常规的 C# 命名,局部变量以小写字母开头,但这是另一回事。)

【讨论】:

  • 现在错误是错误 2 'System.Collections.Generic.IEnumerable' 不包含 'ToList' 的定义和最佳扩展方法重载 'System.Linq .Enumerable.ToList(System.Collections.Generic.IEnumerable)' 有一些无效参数
  • @TechGuy:嗯确实 InvoiceMaster 派生自InvoiceHD?你还没有告诉我们。
  • @TechGuy:那应该没问题。您使用的是哪个版本的 C# 和 .NET?
【解决方案2】:

您返回了错误的类型。您的方法签名表明您正在返回 InvoiceHD 的集合,但实际上您正在返回 InvoiceMaster 的集合

你返回了错误的类型

【讨论】:

    【解决方案3】:

    如果 InvoiceHD 是 Report.Business.ViewModels.InvoiceMaster 的子类型:

    MstDtl.Cast<InvoiceHD>().ToList()
    

    或者如果 ICSNew.Data.InvoiceHD 不是从 Report.Business.ViewModels.InvoiceMaster 派生的,那么您可以手动映射您的数据:

    var MstDtl = from mst in dbMstDtl 
                     select new InvoiceHD //return InvoiceHD instead of 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)
                     }
    

    或将函数的返回类型从 InvoiceHD 更改为

    public IList<Report.Business.ViewModels.InvoiceMaster> GetAllInvoiceMasterDetailsByInvoiceId(int InvoiceId)
    

    【讨论】:

    • 当我使用它时,发生错误无法将“Report.Business.ViewModels.InvoiceMaster”类型的对象转换为“ICSNew.Data.InvoiceHD”。
    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    相关资源
    最近更新 更多