Request for upcoming birthdays

I would like to ask my clients for those whose birthdays are yet to come.

I tried this query, and of course it was unsuccessful:

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

Of course, this will not work correctly (not if you were born in the future), and I would like to know how can I request mine Nullable<DateTime>without a year?

+4
source share
6 answers

You can do this in one line as follows:

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();
+7
source

Try something like this; (it works - I 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();
+1
source

, . , - .

:

  • , , / .
  • , , / .
  • , , .
  • Take the first 15.

I think the C # code would look something like this (you may 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);
+1
source

I am not familiar with Linq, I will try to help with some pseudo-code.

The 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 on December 31st.

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;
}
0
source

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;)

0
source

Here's the same solution as @Aducci, but with NULL and DBFunctions date since EntityFunctions are 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;
0
source

All Articles