LINQ date of birth request to get age

I am running a query (in Northwind db) on the date of birth of employees, and I want to get their age. How should I do it?

    private void Form1_Load(object sender, EventArgs e)
    {
        using (NorthWindDataContext dataContext = new NorthWindDataContext())
        {
            DateTime today = DateTime.Today;

            var q1 = from z in dataContext.Employees
                     select new
                     {
                         z.FirstName,
                       today-z.BirthDate <---- problem  here
                     };

            dataGridView1.DataSource = q1;

        }
    }

in my last grid i want an age column.

Date of birth = 29/05/1969 (example)

+5
source share
5 answers

This can be tricky because you need to make sure your birthday has passed this year, otherwise your age will be missing.

var query = from e in dataContext.Employees
            //get the difference in years since the birthdate
            let years = DateTime.Now.Year - e.BirthDate.Year
            //get the date of the birthday this year
            let birthdayThisYear = e.BirthDate.AddYears(years)
            select new
            {
                //if the birthday hasn't passed yet this year we need years - 1
                Age = birthdayThisYear > DateTime.Now ? years - 1 : years
            };

Executing this method has the advantage of calculating the age in the generated SQL query. This means that you do not need to iterate over the client-side result set to calculate age.

In case you are interested, he will create the following SQL:

-- Region Parameters
DECLARE @p0 Int = 2013
DECLARE @p1 DateTime = '2013-02-14 09:08:46.413'
DECLARE @p2 Int = 1
-- EndRegion
SELECT [t2].[value] AS [years], [t2].[value2] AS [birthdayThisYear], 
    (CASE 
        WHEN [t2].[value2] > @p1 THEN [t2].[value] - @p2
        ELSE [t2].[value]
     END) AS [Age]
FROM (
    SELECT [t1].[value], DATEADD(YEAR, [t1].[value], [t1].[BirthDate]) AS [value2]
    FROM (
        SELECT [t0].[BirthDate], @p0 - DATEPART(Year, [t0].[BirthDate]) AS [value]
        FROM [Employees] AS [t0]
        ) AS [t1]
    ) AS [t2]
+9
source

, 3 : FirstName, Age BirthDate ( , ).

var q1 = (from z in dataContext.Employees
    select new
    {
         FirstName = z.FirstName,
         Age = today.Year - z.BirthDate.Year,
         BirthDate = z.BirthDate
    }).ToList();

q1.ForEach(x => if (x.BirthDate > today.AddYears(-x.Age)) { x.Age--; });

( ) , BirthDate , .

+6

You need the name named, something like this:

private void Form1_Load(object sender, EventArgs e)
{
    using (NorthWindDataContext dataContext = new NorthWindDataContext())
    {
        DateTime today = DateTime.Today;

        var q1 = from z in dataContext.Employees
                 select new
                 {
                     Name = z.FirstName,
                     Age = today - z.BirthDate
                 };

        dataGridView1.DataSource = q1;

    }
}
+1
source
var temp = from o in dataContext.Employees
    select new
    {
         FirstName = o.FirstName,
         Age = DateTime.Now.Year - z.BirthDate.Year -1
    };
+1
source
 var q1 = from z in dataContext.Employees
          select new
         {
            z.FirstName,
            z.BirthDate,
            Age = DateTime.Now.Year - z.BirthDate.Year
         };
0
source

All Articles