please see calculateAge to convert the Birthdate to current age in years in LINQ Query (it works for other keys, ex: EyeColor, but age conversion failing), I would like to compare with the value that was stored in Array. I tried storing the value in DateTime temp variable above the link, right below "case "AgeTo":" but I can not take the value x.profile.BirthDate as it's allowed only within LINQ Query. It appears that LINQ query is not giving error during build, however, it's failing to con
Error:
LINQ to Entities does not recognize the method 'Int32 calculateAge(System.DateTime)' method, and this method cannot be translated into a store expression.
Any help how to handle?
Code and calculateAge method:
if (searchList.Count > 0)
{
foreach (KeyValuePair<String, String> kvp in searchList)
{
switch (kvp.Key)
{
case "AgeFrom":
photosquery = photosquery.Where(x => calculateAge(Convert.ToDateTime(x.profile.BirthDate)) >= Convert.ToInt32(kvp.Value));
break;
case "AgeTo":
photosquery = photosquery.Where(x => calculateAge(Convert.ToDateTime(x.profile.BirthDate)) <= Convert.ToInt32(kvp.Value));
break;
case "EyeColor":
photosquery = photosquery.Where(x => x.physical.EyesColor.Contains(kvp.Value));
break;
}
}
}
//My code for calculateAge:
public static int calculateAge(DateTime birthDay)
{
int years = DateTime.Now.Year - birthDay.Year;
if ((birthDay.Month > DateTime.Now.Month) || (birthDay.Month == DateTime.Now.Month && birthDay.Day > DateTime.Now.Day))
years--;
return years;
}