I get following error when I try to display Age in dataGridView from Birthday properity
LINQ to Entities does not recognize the method 'Int32 CalculateAge(System.Datetime) method, and this method cannot be translated into a store expression
This is the function I use to calculate age:
private int CalculateAge(string birthday)
{
int age;
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;
}
And this is to display on datagradView
var employee = db.Employee.Where(x => x.EmployeeId == id ).
Select(b => new
{
Id = b.EmployeeId,
Namn = b.FirstName + " " + b..LastName,
Age = CalculateAge(b.DOB.ToString()),
Department = b.Department.DepartmentName
} ).Tolist();
if employee != null)
{
dgvEmployee.DataSource = null;
dgvEmployee.DataSource = employee
}
But it doesn't work with CalculateAge function. How can I do to solv this problem? Please Help, and Thank you in advance : Error message is "LINQ to Entities does not recognize the method 'Int32 CalculateAge(System.Datetime) method, and this method cannot be translated into a store expression"