title: LINQ: Unable to cast the type 'System.Int32' to type 'System.Object'
body: <p>I'm new to MVC and EF and LINQ.</p>
  
  <p>I used EF and DB first to create all the models from the DB. I also created a ViewModel "CourseDetailsVM":</p>
  
  <pre><code>public class CourseDetailsVM
  {
      public int CourseId { get; set; }
      public int CategoryFk { get; set; }
      public int SoftwareFk { get; set; }
      public int TeacherFk { get; set; }
      public string CourseCode { get; set; }
      public string CourseCodeMs { get; set; }
      public string CourseName { get; set; }
      public string CourseDescriptionShort { get; set; }
      public int CourseEventId { get; set; }
      public int? CourseEventDurationInDays { get; set; }
      public int? CourseEventPrice { get; set; }
      public int CourseEventDateId { get; set; }
      public DateTime? CourseEventDateTimeFrom { get; set; }
      public DateTime? CourseEventDateTimeTo { get; set; }
  }
  </code></pre>
  
  <p>In my Course Controller I try to get the Model from the Viewmodel using the following query (which works in linqpad but not in mvc) and I also use ViewData to access the repository (this works fine):</p>
  
  <pre><code>// GET: /Course/Details/5
  
      public ActionResult Details(int id)
      {
          // get courses to display course details using ViewData
          ViewData["Courses"] = _repository.GetCourses(id);
  
          // get course events
          var result = (from c in db.Courses
                        join ce in db.CourseEvents
                        on c.CourseId equals ce.CourseFk
                        join ced in db.CourseEventDates
                        on ce.CourseEventId equals ced.CourseEventFk
                        group ce by new { ce.CourseEventId, c.CourseId, c.CourseCode, c.CourseName, ce.CourseEventPrice } into grp
                        select new CourseDetailsVM
                        {
                            CourseEventId = grp.Key.CourseEventId,
                            CourseId = grp.Key.CourseId,
                            CourseCode = grp.Key.CourseCode,
                            CourseEventDateTimeFrom = (from c2 in db.CourseEventDates where (c2.CourseEventFk.Equals(grp.Key.CourseEventId)) select c2.CourseEventDateTimeFrom).Min(),
                            CourseEventDateTimeTo = (from c2 in db.CourseEventDates where (c2.CourseEventFk.Equals(grp.Key.CourseEventId)) select c2.CourseEventDateTimeFrom).Max(),
                            CourseEventDurationInDays = (from c2 in db.CourseEventDates where (c2.CourseEventFk.Equals(grp.Key.CourseEventId)) select c2.CourseEventDateTimeFrom).Count() / 2,
                            CourseName = grp.Key.CourseName,
                            CourseEventPrice = grp.Key.CourseEventPrice,
                            CourseEventDateId = grp.Key.CourseEventId
                        }).OrderBy(c =&gt; c.CourseEventDateTimeFrom);
  
      return View("Details", result);
      }
  </code></pre>
  
  <p>Since Linq compiles on runtime, I can compile the project without any errors. But when accessing the Course/Details view I get the following error:
  <strong>Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.</strong></p>
  
  <p>The error occurs when making an new instance of CourseDetailsVM on the following fields: <code>CourseEventDateTimeFrom, CourseEventDateTimeTo, CourseEventDurationInDays</code></p>
  
  <p>I tried fill the mentioned properties using hard coded values and it worked fine. So I know, where the error occurs, but I don't know why and how to solve it.</p>
  
  <p>As requested by Yuliam Chandra, the content of the CourseEventDate class (generated by EF):</p>
  
  <pre><code>public partial class CourseEventDate
  {
      public int CourseEventDateId { get; set; }
      public Nullable&lt;int&gt; CourseEventFk { get; set; }
      public Nullable&lt;System.DateTime&gt; CourseEventDateTimeFrom { get; set; }
      public Nullable&lt;System.DateTime&gt; CourseEventDateTimeTo { get; set; }
      public virtual CourseEvent CourseEvent { get; set; }
  }
  </code></pre>
  
  <p>I hope that sb of you guys can help me.</p>
  
  <p>Thank you,</p>
  
  <p>Ronin</p>