Cannot implicitly convert linq result to viewmodel list

I am new to MVC and struggle to implement ViewModel to query multiple tables. Initially, my setup worked fine, but now, reloading the project, I get a compilation error, copied below:

Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'

ViewModel Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TestProject.Models
{
  public class COMPCATEGORY
  {
    public List<COMP> Comp { get; set; }
    public List<CATEGORY> Category { get; set; }
  }
}

Controller Code:

namespace TestProject.Controllers
{
    public class COMPsController : Controller
    {
        private mattbeaneyEntities1 db = new mattbeaneyEntities1();

        // GET: COMPs
        public ActionResult Index()
        {
          COMPCATEGORY viewModel = new COMPCATEGORY();
          viewModel.Category = db.CATEGORies.ToList();
          viewModel.Comp = db.COMPs.ToList();
          return View(viewModel);
        }

DB Context Code:

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; }
  }
+4
source share
1 answer

As the error shows, it has problems with two types with the same name when we expect it only. The namespace is our clue here:

Cannot implicitly convert type 'System.Collections.Generic.List<CATEGORY>' to 'System.Collections.Generic.List<TestProject.Models.CATEGORY>'

Usually there are a few things that I like in this case:

-: , ? , !

: ? - , DLL bin, - !

+2

All Articles