0

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?

user1666620
  • 4,800
  • 18
  • 27
Phill Sanders
  • 487
  • 2
  • 10
  • 30
  • you can't do a parse in a LinqToSQL query since it needs to be translated into SQL, and to the best of my knowledge there isn't an SQL function for parse. The exception message explicitly states this: `LINQ to Entities does not recognize the method 'System.Decimal ToDecimal(Int32)' method, and this method cannot be translated into a store expression.` – user1666620 Sep 17 '15 at 09:18
  • Is there a work around? – Phill Sanders Sep 17 '15 at 09:25
  • possible duplicate of [Select parsed int, if string was parseable to int](http://stackoverflow.com/questions/4961675/select-parsed-int-if-string-was-parseable-to-int) check Jon Skeet's answer – user1666620 Sep 17 '15 at 09:26
  • The simple workaround is to select counts, then call `.AsEnumerable`, then (in the next `Select`) cast/convert them to `decimal` and divide – tukaef Sep 17 '15 at 09:28
  • How would that be achieved in the code, sorry relatively new to this – Phill Sanders Sep 17 '15 at 09:37

1 Answers1

2

All of that code boils down to "I want 3 / 2 to return 1.5", right? Cast to a floating point data type, for example double:

(double)myCount1 / (double)myCount2 * 100.0

Entity Framework still does not recognize the Convert.* methods. They are to be avoided anyway. They are a code smell. Usually, a normal cast is the right answer because it is simple.

usr
  • 168,620
  • 35
  • 240
  • 369