开发者

Linq query-dateOfBirth to age

开发者 https://www.devze.com 2022-12-18 09:36 出处:网络
Is is possible to turn a dateOfBirth in a linq query to an age within the query so i can do a comparis开发者_开发问答on to an int?

Is is possible to turn a dateOfBirth in a linq query to an age within the query so i can do a comparis开发者_开发问答on to an int?

Cheers in advanced

Truez


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消