title: How to convert ObjectSet from Entities to IEnumerable of Models?
body: <p>I am building a test application using MVC3, Razor, and Entity Framework 4.1 with a schema-first approach (as apposed to a code-first approach), in a repository pattern. I would like to avoid accessing data objects in my view, and access a model instead, but I am having a problem. As far as I can tell, the data objects are being returned from the data layer as ObjectSet, but my View needs IEnumerable, and I don't know how to cast one to the other.</p>
  
  <p>Here is some code, to help clarify.</p>
  
  <hr>
  
  <p><strong>Model ...</strong></p>
  
  <pre><code>namespace TestSolution.Models
  {
      public class ProjectModel
      {
          [HiddenInput]
          public int Id { get; set; }
  
          [Required]
          [StringLength(255, ErrorMessage = "The name cannot be more than 255 characters long.")]
          [Display(Name = "Name")]
          public string Name { get; set; }
  
          [Required]
          [Display(Name = "Description")]
          public string Description { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>Repository ...</strong></p>
  
  <pre><code>public IQueryable&lt;ProjectModel&gt; GetProjects()
  {
      return Db.Project;
  }
  </code></pre>
  
  <p><strong>Entities ...</strong></p>
  
  <pre><code>public ObjectSet&lt;Project&gt; Project
  {
      get
      {
          if ((_Project == null))
          {
              _Project = base.CreateObjectSet&lt;Project&gt;("Project");
          }
          return _Project;
      }
  }
  </code></pre>
  
  <p><strong>Controller ...</strong></p>
  
  <pre><code>public ActionResult Index()
  {
      IEnumerable&lt;TestSolution.Models.ProjectModel&gt; model = _projectRepository.GetProjects();
      return View(model);
  }
  </code></pre>
  
  <p><strong>View ...</strong></p>
  
  <pre><code>@model IEnumerable&lt;TestSolution.Models.ProjectModel&gt;
  </code></pre>
  
  <p><strong>Error I am getting when building ...</strong></p>
  
  <pre><code>Cannot implicitly convert type 'System.Data.Objects.ObjectSet&lt;TestSolution.Project&gt;' to 'System.Linq.IQueryable&lt;TestSolution.Models.ProjectModel&gt;'. An explicit conversion exists (are you missing a cast?)
  </code></pre>
  
  <hr>
  
  <p>Does this question make sense? I am just not sure where go from here ... any advise you guys can give me would be awesome. :)</p>
  
  <p><strong>EDIT:</strong> I was able to solve this with Kyle's suggestion by changing my Repository code to ...</p>
  
  <pre><code>public IQueryable&lt;ProjectModel&gt; GetProjects()
  {
    return Db.Project.Select(i =&gt; new ProjectModel() { Id = i.Id, Name = i.Name, Description = i.Description });
  }
  </code></pre>