-1

I get this error message when I load all employees when I load employees in dataGridView form.

"LINQ to Entities Does Not Recognize the Method 'int32 CalculateAge(Int32)' method, and this methode cannot be translate into a store expression"

This is how I populate them in dataGridView

private void LoaddEmployees() {
   try
    {
        db = new EmployeeEntities();

        var employees = (from u in db.Users 
                         select new
                         {
                             EmployeeId = u.EmployeeId,
                             UserName = u.UserName,
                             FirstName = u.FirstName,
                             LastName = u.LastName,
                             Birthday = u.Birthday,
                             Age = CalculateAge(u.EmployeeId) // Calling CalculateAge method
                         }).ToList();
        dgvEmployeesList.DataSource = employees;                  
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
} 

And here is my CalculateAge Method that I have problem with. The very calculation (Mathematics) is working but get error when I load my dataGridview.

 private int CalculateAge(int employeeId) {

    int age;
    var employeeAge = db.Users.Where(x => x.EmployeeId == employeeId).FirstOrDefault();

            DateTime empBirthday = Convert.ToDateTime(empAge.Birthday);
            DateTime today = DateTime.Today;
            age = today.Year - empBirthday.Year;
            if (empBirthday > today.AddYears(-age)) 
                age--;
                return age; 
}

When page loading then I get the above error:

Please I need your help to solve this problem. I really do not understand what is wrong with my code.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Emma
  • 23
  • 5
  • 3
    Possible duplicate of [LINQ to Entities does not recognize the method](https://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method) – Selvin Apr 19 '19 at 10:30

2 Answers2

0
 var employees = (from u in db.Users 
                                 select new
                                 {
                                     EmployeeId = u.EmployeeId,
                                     UserName = u.UserName,
                                     FirstName = u.FirstName,
                                     LastName = u.LastName,
                                     Birthday = u.Birthday,
                                     Age = CalculateAge(u.EmployeeId) // Calling CalculateAge method
                                 })

This part of your code is converted into SQL query by Entity Framework and sadly your C# method CalculateAge(u.EmployeeId) can't be converted to SQL. You can't use that kind of custom methods with IQueryable. First retrieve data then apply method or do same calculation on SQL side (for example stored procedure).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
George Human
  • 101
  • 1
  • 13
0

You can convert db.Users to list and then run evaluate your age calculation. The code would look something like

private void LoaddEmployees()
    {
        try
        {
            db = new EmployeeEntities();

            var employees = (from u in db.Users.ToList()
                             select new
                             {
                                 EmployeeId = u.EmployeeId,
                                 UserName = u.UserName,
                                 FirstName = u.FirstName,
                                 LastName = u.LastName,
                                 Birthday = u.Birthday,
                                 Age = CalculateAge(u.Birthday) // note that we are sending in the Birthday
                             }).ToList();
            dgvEmployeesList.DataSource = employees;


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

And then update the CalculateAge method as

private int CalculateAge(string birthday)
    {
        if (string.IsNullOrWhiteSpace(birthday)) return 0;

        DateTime empBirthday = Convert.ToDateTime(birthday);
        DateTime today = DateTime.Today;
        age = today.Year - empBirthday.Year;
        if (empBirthday > today.AddYears(-age))
            age--;
        return age;
    }
josejamesp
  • 52
  • 3