Select record having age between 18 and 21 using Linq query in Entity Framework

Last Reply 7 days ago By mukesh1

Posted 7 days ago

i m finding user according to age, but in database i have only date of birth, my dob in database is date type.stores values like 1990-08-22,

in need to find users from age 18 to age 25

Posted 7 days ago Modified on 7 days ago

 

var year = DateTime.Now.Year;
var month = DateTime.Now.Month;
var day = DateTime.Now.Day;
string dob1= dobyear1 + "-" + month + "-" + day;
DateTime dob21 = Convert.ToDateTime(dob1);
var dobyear2 = year - 25;
string dob2 = dobyear2 + "-" + month + "-" + 1;
DateTime dob22 = Convert.ToDateTime(dob2);

var userslist = db.Users.Where(s => s.Gender == "Female" && s.DOB <= dob21 && s.DOB >= dob22).ToList();

It's working