0

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"

Anna
  • 69
  • 1
  • 10
  • 3
    Unrelated to the question, but assuming `DOB` is a `DateTime` (maybe a `DateTime?`), why are you converting it to a `String` (`b.DOB.ToString()`) and then converting it to a `DateTime` again (`Convert.ToDateTime(birthday)`)? – Anderson Pimentel Apr 10 '20 at 23:36
  • @ Anderson Pimentel Thank you for your response, but I have tried even with Datetime but geting the same problem – Anna Apr 11 '20 at 00:08

1 Answers1

2

Try querying the data you need, then converting the collection to IEnumerable<T>, then using the CalculateAge() method.

var employee = db.Employee.Where(x => x.EmployeeId == id ).
               Select(e => new { e.EmployeeId, e.FirstName, e.LastName, e.DOB, e.Department.DepartmentName }).
               AsEnumerable().
               Select(b => new
               {
                  Id = b.EmployeeId, 
                  Name = b.FirstName + " " + b.LastName,
                  Age = CalculateAge(b.DOB.ToString()),
                  Department = b.DepartmentName
                  } ).Tolist();
if employee != null)
{
   dgvEmployee.DataSource = employee;
}
Anthony McGrath
  • 792
  • 5
  • 7
  • Thank you for your response. I forgot to display Department name, (I have edited my question) I have DepartmentId in my employee class as foreignKey to Department yable. So if you edit you code , then How should it be? I put it in e.Department.DepartmentName but in select => b it doesn't work b,Department.DepartmentName – Anna Apr 11 '20 at 00:07
  • Hi, I made an edit based on your comment. Maybe that will work. – Anthony McGrath Apr 11 '20 at 00:12
  • `dgvEmployee.DataSource = null` is redundant, you don't need it. – Fabio Apr 11 '20 at 00:32
  • @Anthony McGrath , thank you very much, it's working perfect. Thank you again – Anna Apr 11 '20 at 16:12