Am using C# MVC and i need to get the user details who having birthday with in next 20 days. using linq to sql query which wants to compare only date and month and not the year, to fetch the users who having birthday within next 20days, anyone kindly help me with the linq to sql query to get the users who having birthday within next 20 days.
thanks in advance,
Why not store the Birthday in a local variable, change the year to the current year and then check whether it occurs in the next 20 days?
public bool IsBirthdayInNextTwentyDays(DateTime actualBirthday)
{
var birthday = actualBirthday;
birthday.Year = DateTime.Now.Year;
return birthday > DateTime.Now && birthday < DateTime.Now.AddDays(20);
}
Then in Linq something like:
user.Where(u => IsBirthDayInNextTwentyDays(u.Birthday));
Kindness,
Dan
This is the good solution.
public bool IsBirthdayInNextTwentyDays(DateTime today,DateTime actualBirthday,int days)
{
if ((actualBirthday.DayOfYear - today.DayOfYear >= 0) )
{
return (actualBirthday.DayOfYear - today.DayOfYear <= days);
}
else
{
return (actualBirthday.DayOfYear +365 - today.DayOfYear <= days);
}
}
SPS Win in Sharing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With