C# 无法将linq结果隐式转换为viewmodel列表

C# 无法将linq结果隐式转换为viewmodel列表,c#,asp.net,asp.net-mvc,linq,C#,Asp.net,Asp.net Mvc,Linq,我是MVC新手,正在努力实现一个ViewModel来查询多个表。最初,我的设置工作得很好,但现在重新加载项目后,我得到一个编译错误,如下所示: Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>' 数据库上下文代码: using System;

我是MVC新手,正在努力实现一个ViewModel来查询多个表。最初,我的设置工作得很好,但现在重新加载项目后,我得到一个编译错误,如下所示:

Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'
数据库上下文代码:

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;


  public partial class mattbeaneyEntities1 : DbContext
  {
    public mattbeaneyEntities1()
      : base("name=mattbeaneyEntities1")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<CATEGORY> CATEGORies { get; set; }
    public virtual DbSet<COMP> COMPs { get; set; }
  }
使用系统;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类mattbeaneyEntities1:DbContext
{
公共MattbeaneyEntities 1()
:base(“name=mattbeaneyEntities1”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共虚拟数据库集类别{get;set;}
公共虚拟数据库集组件{get;set;}
}

正如错误所示,它在使用两种同名类型时遇到问题,而我们只需要一种。名称空间是我们的线索:

Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”
在这种情况下,我通常会检查几件事:

第一:是否有两个类共享相同的名称并属于不同的名称空间?随着项目的发展,这很容易做到


次要:主项目名称空间是否已更改?有时,由于一些重构,我们更改了项目名称,然后在bin文件夹中得到了两个.dll文件,其中包含了我们所有类的副本-删除旧的一个

看起来您有两个名为category的类。在模型中,还有一个在实体框架声明中??是的,就是这样,出于某种原因,我有一个类,其中CATEGORY和COMP是分部类。取消了这个,把它们作为单独的完整类。效果不错。请将您的解决方案作为问题的答案发布,并将其标记为答案。这将帮助将来可能遇到此问题的其他人进行搜索。@SaagarEliasJacky我回滚了您错误批准的编辑建议,因为您将错误消息更改为“无法隐式转换类型”System.Collections.Generic.List“为”System.Collections.Generic.List“。原来的问题不是这样的。将代码转换为非代码时请注意:
@MattBeaney轻松完成@基思佩恩;你完全正确!完成:)
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;


  public partial class mattbeaneyEntities1 : DbContext
  {
    public mattbeaneyEntities1()
      : base("name=mattbeaneyEntities1")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<CATEGORY> CATEGORies { get; set; }
    public virtual DbSet<COMP> COMPs { get; set; }
  }
Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'