Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Birthday reminder Linq Query ignoring year

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,

like image 423
kart Avatar asked Oct 22 '09 06:10

kart


2 Answers

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

like image 93
Daniel Elliott Avatar answered Nov 11 '22 11:11

Daniel Elliott


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

like image 1
user1779178 Avatar answered Nov 11 '22 11:11

user1779178