How do I get persons whose age is over 21 using EF?
from person in context.Persons
where DateTime.UtcNow - person.Dob > TimeSpan.FromHours(184086)
select new {person, Age = DateTime.UtcNow - person.Dob}
The above failed.
What can I do to fix this? Is the above supported in the more recent version of EF eg. 4.2+
Can I access the properties of the DateTime struct as follow?
from person in context.Persons
where DateTime.UtcNow.Year - person.Dob.Year > 21
select new {person, Age = DateTime.UtcNow.Year - person.Dob.Year}
You can use the
<operator in linq-to-entites queries. You can’t use addition or subtraction though. Fortunately,SqlFunctionshas functions to calculate date differences. So what you can do is this:You have to create the variable
d21first, otherwise EF complains about not knowingAddYears.