我如何求解不能隐式转换type system.collections.generic.list



我在c#中有这个模型:

public class interfaceModel
{
    public string interfacename { get; set; }
    public long  id { get; set; }
}

并编写此代码:

public static List<interfaceModel> CompanyInterfaceFetch()
{
    var database = new DataBaseDataContext();
    var companyinterface = new CompanyInterface();
    var interfacelist =
                database.CompanyInterfaces.Select(x => new interfaceModel { interfacename = x.InterfaceName, id=x.id});
    return interfacelist.ToList();
}
// database model
public class interfaceModel
{
    public string interfacename { get; set; }
    public long id { get; set; }
}

现在我编写代码来调用该方法:

public static List<interfaceModel> interfacefetch()
{
    return Database.CompanyInterfaceFetch();
}

但是在这条线上:

return Database.CompanyInterfaceFetch();

我得到此错误:

错误CS0029:无法隐式将类型'System.Collections.generic.list'转换为'System.Collections.generic.list

我该如何解决这个问题?谢谢。

您有两个单独的类,具有相同名称。判断命名,这是两者的位置:

  • 控制器>数据库> InterfaceModel
  • 模型> InterfaceModel

这两种类型的名称相同,甚至可能具有完全相同的字段,但无法交换。删除它们的任何一个然后再次编译以检查错误以查看您必须进行的调整。

您的错误很明显:

错误CS0029无法隐式转换

类型'System.Collections.Generic.List<Web.Controllers.Database.interfaceModel>'

'System.Collections.Generic.List<Web.Models.interfaceModel>'

您在这两个位置有两种相同名称。

如果它们具有相同的字段,则应删除其中一个。如果它们不同,则需要修复代码中的导入,以便CompanyInterfaceFetch()interfacefetch()参考同一类型。如有必要

由于您有两种类型分布在不同的名称空间中,因此不能从一种类型转换为另一种类型。

对我来说,它看起来还不错,从设计角度来看!

解决此问题的方法之一就是使用汽车应用程序!

最新更新