LINQ查询出生日期以获得年龄

时间:2013-02-14 08:59:24

标签: c# linq-to-sql

我正在员工出生日期进行查询(在northwind db中),我希望得到他们的年龄。我怎么做?

    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;

        }
    }

在我的最终网格中,我想要一个年龄段。

birthdate = 29/05/1969(例子)

5 个答案:

答案 0 :(得分:12)

这可能很棘手,因为你需要确保今年过生日,否则你的年龄将会超过一岁。

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

这样做的好处是可以在生成的SQL查询中计算年龄。这意味着您无需在客户端迭代结果集来计算年龄。

如果您感兴趣,它将生成以下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]

答案 1 :(得分:6)

创建具有3个属性的匿名对象:FirstNameAgeBirthDate(这需要检查员工生日是否已过去今年)。

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是否已经通过,所以选择数据后你必须检查它。

答案 2 :(得分:1)

您需要一个命名属性,如下所示:

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;

    }
}

答案 3 :(得分:1)

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

答案 4 :(得分:0)

 var q1 = from z in dataContext.Employees
          select new
         {
            z.FirstName,
            z.BirthDate,
            Age = DateTime.Now.Year - z.BirthDate.Year
         };