tags:

views:

24

answers:

1

Is is possible to turn a dateOfBirth in a linq query to an age within the query so i can do a comparison to an int?

Cheers in advanced

Truez

+1  A: 

It would be tricky or expensive to calculate the exact age for each record. Assuming you're really wanting to say something like "find me everyone 18 or over" it would be a better idea to work out the date of birth of someone who is exactly 18 today, and find everyone with a date of birth less than or each to that:

DateTime latestAdultBirth = DateTime.Today.AddYears(-18);
var adults = people.Where(person => person.dateOfBirth < latestAdultBirth);
Jon Skeet