Reputation: 7950
I'm new to MVC and EF and LINQ.
I used EF and DB first to create all the models from the DB. I also created a ViewModel "CourseDetailsVM":
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; }
}
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):
// 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 => c.CourseEventDateTimeFrom);
return View("Details", result);
}
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: Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
The error occurs when making an new instance of CourseDetailsVM on the following fields: CourseEventDateTimeFrom, CourseEventDateTimeTo, CourseEventDurationInDays
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.
As requested by Yuliam Chandra, the content of the CourseEventDate class (generated by EF):
public partial class CourseEventDate
{
public int CourseEventDateId { get; set; }
public Nullable<int> CourseEventFk { get; set; }
public Nullable<System.DateTime> CourseEventDateTimeFrom { get; set; }
public Nullable<System.DateTime> CourseEventDateTimeTo { get; set; }
public virtual CourseEvent CourseEvent { get; set; }
}
I hope that sb of you guys can help me.
Thank you,
Ronin
Upvotes: 2
Views: 3878
Reputation: 14640
The error could be caused when comparing CourseEventFk
(int?
) and CourseEventId
(int
) using Equals
where its parameter accepts object type which is a CLR method. LINQ to Entities doesn't support CLR method.
Try to use ==
operator, which somewhat compatible to L2S, on following code.
CourseEventDateTimeFrom = ..where (c2.CourseEventFk == grp.Key.CourseEventId)..
CourseEventDateTimeTo = ..where (c2.CourseEventFk == grp.Key.CourseEventId)..
CourseEventDurationInDays = ..where (c2.CourseEventFk == grp.Key.CourseEventId)..
Upvotes: 5
Reputation: 2701
What I did in the past when I have encountered this kind of error was to create a static object (or anything that I needed at the given time) and did conversion myself such as following:
Private static double? myDoubleConversion(string sInput)
{
//conversion logic comes here
return result;
}
Upvotes: 0