简体   繁体   English

获取生日提醒Linq Query无视年份

[英]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. 我正在使用C#MVC,我需要获取在未来20天内生日的用户详细信息。 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. 使用linq to sql查询只想比较日期和月份而不是年份,为了获取在接下来的20天内生日的用户,任何人都可以帮助我使用linq to sql查询来获取在接下来的20天内生日的用户。

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? 为什么不将Birthday存储在本地变量中,将年份更改为当前年份,然后检查它是否在接下来的20天内发生?

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: 然后在Linq中:

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 SPS赢得分享

Here's one way to do it. 这是一种方法。 I don't really like the way it computer "this year's" birthday first and then correct it if it already passed, but I couldn't think of a better way in short time. 我真的不喜欢计算机“今年的”生日那天的方式,然后如果它已经过去就更正它,但我想不出更好的方法在短时间内。

from p in Persons
let thisYearsBirthday = p.Birthdate.AddYears(today.Year - p.Birthdate.Year)
// OR this way, although the SQL it produces it a little less simple
// let thisYearsBirthday = new DateTime(today.Year, p.Birthdate.Month, p.Birthdate.Day)
let nextBirthday = (thisYearsBirthday >= today) ? thisYearsBirthday : thisYearsBirthday.AddYears(1)
where nextBirthday >= today && nextBirthday <= today.AddDays(20)
select new { /* ... */ };

Use the DateTime.DayOfYear property to get an integer; 使用DateTime.DayOfYear属性获取整数; 1st January == 1, last day of year = 365/366. 1月1日== 1,一年的最后一天= 365/366。

The naive versions uses something like 天真的版本使用类似的东西

where user.Birthday.DayOfYear - DateTime.Now.DayOfYear > 20

This doesn't work when the year wraps round -- where the current day is in late december and the user's birthday is in early january. 这一年不会起作用 - 当天是在12月下旬,而用户的生日是在1月初。 But start with DateTime.DayOfYear 但是从DateTime.DayOfYear开始

public static bool IsBirthdayInNextXDays(DateTime realDate, DateTime birthdayDate, int numberOfDaysToCheck)
{
    bool isOk = false;

    if ((birthdayDate.DayOfYear - realDate.DayOfYear) <= numberOfDaysToCheck && (birthdayDate.DayOfYear - realDate.DayOfYear) >= 0)
        isOk = true;

    return isOk;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM