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.