Query upcoming birthdays

喜夏-厌秋 提交于 2019-12-14 03:46:19

问题


I'd like to query my customers for the ones, whose birthdays are yet to come.

I've tried this query, and it - of course - has failed breathtakingly:

Addresses.Where(adr => adr.DateOfBirth != null && adr.DateOfBirth.Value >
DateTime.Now).Take(15).ToList();

Of course this can't work properly (not if you're born in the future) and I'd like to know how I can query my Nullable<DateTime> without a year?


回答1:


You can do it in one line like this:

context.Addresses.Where(adr => adr.DateOfBirth != null).OrderBy(adr => EntityFunctions.DiffDays(DateTime.Today, EntityFunctions.AddYears(adr.DateOfBirth, EntityFunctions.DiffYears(adr.DateOfBirth, DateTime.Today) + ((adr.DateOfBirth.Month < DateTime.Today.Month || (adr.DateOfBirth.Day <= DateTime.Today.Day && adr.DateOfBirth.Month == DateTime.Today.Month)) ? 1 : 0)))).Take(15).ToList();

Or in a more readable format:

var query = from adr in context.Addresses
            where adr.DateOfBirth != null
            let diffYears = EntityFunctions.DiffYears(adr.DateOfBirth, DateTime.Today)
            let birthdayOccurred = adr.DateOfBirth.Month < DateTime.Today.Month || (adr.DateOfBirth.Day <= DateTime.Today.Day && adr.DateOfBirth.Month == DateTime.Today.Month)
            let nextBirthdate = EntityFunctions.AddYears(adr.DateOfBirth, diffYears + (birthdayOccurred ? 1 : 0))
            let daysToBirthdate = EntityFunctions.DiffDays(DateTime.Today, nextBirthdate)
            orderby daysToBirthdate
            select adr;

var result = query.Take(15).ToList();



回答2:


I'm not sure you can do this as a one-liner. Certainly not with any degree of clarity.

The needed steps are:

  1. Create an ordered list containing only the birthdates where the month/day comes after today.
  2. Create an ordered list containing only the birthdates where the month/day is before today.
  3. Append the second list to the first one, you now have a single list, sorted in birthday order.
  4. Take the first 15.

I think the C# code would look something like this (You might need to add a List or two.)

var list1 = Addresses.Where(adr => adr.DateOfBirth != null && (adr.DateOfBirth.Value.Month > DateTime.Today.Month || (adr.DateOfBirth.Value.Month == DateTime.Today.Month && adr.DateOfBirth.Value.Day >= DateTime.Today.Day))).ToList();
var list2 = Addresses.Where(adr => adr.DateOfBirth != null && (adr.DateOfBirth.Value.Month < DateTime.Today.Month || (adr.DateOfBirth.Value.Month == DateTime.Today.Month && adr.DateOfBirth.Value.Day < DateTime.Today.Day))).ToList();
var fullList = list1.Add(list2);



回答3:


Try something like this; (this is working - I've tested)

//A mock up value for comparison (as DayOfYear not supported in Linq)
int doy = DateTime.Today.Month * 31 + DateTime.Today.Day;

var results = Addresses.Where(a => a.DateOfBirth != null)
             .OrderBy( a => 
             (a.DateOfBirth.Value.Month * 31 + a.DateOfBirth.Value.Day) +
  (a.DateOfBirth.Value.Month * 31 + a.DateOfBirth.Value.Day > doy ? 0 : 400 ))
             .Take(15).ToList();



回答4:


Im not familiar with Linq, I'll try to help with some pseudocode.

Most important condition should look like this:

Date(CurrentDate.Year, DateOfBirth.Month, DateOfBirth.Day) >= CurrentDate 
|| ' OR
Date(CurrentDate.Year + 1, DateOfBirth.Month, DateOfBirth.Day) >= CurrentDate

This will work 31 December too.

Alternative:

// consider this as pseudocode, I didnt tested that in C#

function GetNextBirthday(DateTime DateOfBirth)
{
int yearOfNextBirthday;

int birthdayMMDD = DateOfBirth.Month*100 + DateOfBirth.Day;
int todayMMDD = CurrentDate.Month*100 + CurrentDate.Day;

    if (birthdayMMDD >= todayMMDD)
    {
        yearOfNextBirthday = CurrentDate.Year; // this year
    }
    else
    {
        yearOfNextBirthday = CurrentDate.Year + 1; // next year
    }

DateTime nextBirthday; 

// you have to write this line yourself, i dont remember how to make date from Y, M, D
nextBirthday = DateFromYMD(yearOfNextBirthday, DateOfBirth.Month, DateOfBirth.Day);

return nextBirthday;
}



回答5:


Here is my entry for "one liner":

DateTime now = DateTime.Now;
var next15 =
    from a in db.Addresses
    where a.DateOfBirth != null
    let diff = EntityFunctions.DiffSeconds(
        EntityFunctions.AddYears(now, -EntityFunctions.DiffYears(a.DateOfBirth, now)),
        a.DateOfBirth)
    orderby diff >= 0 ? diff : diff + 366*24*60*60
    select new a;

var res = next15.Take(15).ToList();

Just tested with SQL Database - and it works like charm ;)




回答6:


Here's the same solution as @Aducci but with nullable date and the DBFunctions, as EntityFunctions became deprecated.

          pacientes = from paciente in pacientes
                where paciente.Nascimento != null
                let diffYears = DbFunctions.DiffYears(paciente.Nascimento, DateTime.Today)
                let birthdayOccurred = paciente.Nascimento.Value.Month < DateTime.Today.Month 
                || (paciente.Nascimento.Value.Day <= DateTime.Today.Day && paciente.Nascimento.Value.Month == DateTime.Today.Month)
                let nextBirthdate = DbFunctions.AddYears(paciente.Nascimento, diffYears + (birthdayOccurred ? 1 : 0))
                let daysToBirthdate = DbFunctions.DiffDays(DateTime.Today, nextBirthdate)
                orderby daysToBirthdate
                select paciente;


来源:https://stackoverflow.com/questions/15118968/query-upcoming-birthdays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!