I have the following get method:
public ActionResult InstructorEventsAttended()
{
//Populate the list
var instructors = from s in db.Instructors
orderby db.InstructorEvents.Where(x => x.InstructorId == s.InstructorId && x.AttendanceId == 4).Count() descending
select s;
var viewModel = instructors.Select(t => new StatsInstructorEventsAttendedViewModel
{
Name = t.FirstName + " " + t.LastName,
EventsAttendedF = db.InstructorEvents.Where(x => x.InstructorId == t.InstructorId && x.AttendanceId == 4 && x.Event.EventTypeId == 1).Count(),
EventsAttendedFPer = (db.InstructorEvents.Where(x => x.InstructorId == t.InstructorId && x.AttendanceId == 4 && x.Event.EventTypeId == 1).Count() / db.Events.Where(x => x.EventDate >= t.Joined && x.EventTypeId == 1 && x.EventStatusId == 2).Count()) * 100,
});
return PartialView("_InstructorEventsAttended", viewModel);
}
The view model is:
public class StatsInstructorEventsAttendedViewModel
{
public int InstructorId { get; set; }
[Display(Name = "Name")]
public string Name { get; set; }
[Display(Name = "Fundraising")]
public decimal EventsAttendedF { get; set; }
[Display(Name = "Fundraising")]
public decimal EventsAttendedFPer { get; set; }
}
However the Initialiser for EventsAttendedPer calculates as zero because the first count is smaller than the second. I'm aware that the way to fix this is to convert the numbers to decimal but when I try Convert.ToDecimal I get an error:
System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Decimal ToDecimal(Int32)' method, and this method cannot be translated into a store expression.
How can I convert the Counts to Decimal to allow the correct division result?