Skip to main content
 首页 » 编程设计

c#之无法将类型 'System.Collections.Generic.Listwebsite.Class1>' 转换为 'System.Collections.Generic.Listlibrary.Class1>'

2024年02月06日28Renyi-Fan

我在 .net 4.5 上使用 MVC。

我已将模型从网站分离到类库中,并将 .edmx 的 tt 类文件复制到我的网站,以便我可以访问模型类。

尽管当我像这样从库中返回一些数据时,

List<website.Class1> a = objLibraryRef.GetFileLists1(); 

我收到这个错误。

Cannot convert type 'System.Collections.Generic.List<website.Class1>' 
to 'System.Collections.Generic.List<library.Class1>' 

我不想引用类库来访问模型类。

如有任何帮助,我们将不胜感激。 谢谢和干杯。

请您参考如下方法:

您的 DAL 方法正在返回 List<T>类型 Class1这是在 library命名空间,同时将其分配给 Class1 类型但它的命名空间不同,即 website1不是 library .

您可以使用隐式类型变量来推断 DAL 库方法返回的类型:

var list = objLibraryRef.GetFileLists1(); 

或者如果你真的想要 website.Class1 类型的列表然后你必须按照@adricadar 在他的帖子中建议的那样再次投影到列表上。