在 linq to sql 中按即将到来的生日获取数据

Fetch data by upcoming birthdays in linq to sql

我想显示未来 15 天生日的人的列表。我的 table 列中有以下日期:

让我更清楚我的问题

下面是我的员工 TABLE 列

EMP_ID  |EMP_TYPE   |EMP_USERNAME       |EMP_DOB
======= |========== |===============    |==================
1       |ADMIN      |ELENA GILBERT      |1993-02-19
2       |EMPLOYEE   |KATHERINE PIERCE   |1993-03-19
3       |EMPLOYEE   |STEFAN SALVATORE   |1993-04-19
4       |EMPLOYEE   |DAMON SALVATORE    |1993-05-19
5       |EMPLOYEE   |JEREMY GILBERT     |1993-05-20

现在我只想显示 15 天后即将到来的生日。 下面我创建了一个自定义 class,我在其中设置了两个属性:

public class Birthday
{
   public string Name { get; set; }
   public DateTime date { get; set; }
}

下面是我的 Web 方法,其中 return 我需要一个列表,我只需要 Emp_Username 和 Emp_DOB,它们将在 15 天内发布。

[WebMethod]
public static List<Birthday> getBirthday()
{
   var slist = new List<Birthday>();
   var db = new BLUEPUMPKINEntities();
   var query = (from emp in db.Employees
   let BirthdayDiff = (new DateTime(DateTime.Now.Year,
                      emp.EMP_DOB.Value.Month, emp.EMP_DOB.Value.Day) -                                                                                                         DateTime.Now).TotalDays where BirthdayDiff >= 0 && BirthdayDiff <= 15
                      select new Birthday { Name = emp.EMP_USERNAME, date = Convert.ToDateTime(emp.EMP_DOB) });
                      return slist.ToList();
    }

问题 是我上面的代码不工作并且在调试中没有显示任何错误。

您应该将查询更改为类似这样的内容,然后 return 它:

[WebMethod]
public static List<Employee> getBirthday()
{
    var db = new BLUEPUMPKINEntities();
    const int dateOffset = 15;

    var today = DateTime.Today;
    var maxDate = DateTime.Today.AddDays(dateOffset);

    return (from emp in db.Employees
            where emp.EMP_DOB.Value >= today
            where emp.EMP_DOB.Value <= maxDate
            select emp).ToList();
}

您的代码中至少有 三个 个问题。


首先,这一行可能会产生不正确的结果:

let BirthdayDiff = (new DateTime(DateTime.Now.Year, emp.EMP_DOB.Value.Month, emp.EMP_DOB.Value.Day) - DateTime.Now).TotalDays

请注意,您使用 当前时间 生成

DateTime.Now.Year

并考虑以下情况:

Now is 25-Dec-15 and One of your Employee is having birthday in 3-Jan-16. According to the calculation, you would produce DateTime with value of 3-Jan-15 for your Employee and you minus it with DateTime.Now and thus you will get value < -300 in total days.


其次,不要在单个查询中使用DateTime.Now超过一次,因为后续DateTime.Now的结果可能与第一。仅使用一次:

DateTime now = DateTime.Now; //and then just use now

或者更好的是,消除所有小时和分钟的差异:

DateTime today = DateTime.Today;

最后,您永远不会 return 查询结果,而只是一个空列表。

注意你定义:

var slist = new List<Employee>();

以及查询:

var db = new BLUEPUMPKINEntities();
var query = from emp in db.Employees
            let BirthdayDiff = (new DateTime(DateTime.Now.Year, emp.EMP_DOB.Value.Month, emp.EMP_DOB.Value.Day) - DateTime.Now).TotalDays
            where BirthdayDiff >= 0 && BirthdayDiff <= 15
            select emp;

但是您既没有将 slistquery 联系起来,也没有将 return 与 query 本身联系起来。因此,你总是什么也得不到,因为 slist 总是空的,new List.


小修改:db 更改为 db.Employees 并添加 ToList()

纠正其中三个,你就有了一个安全的方法来得到你想要的(注意:小心闰年):

[WebMethod]
public static List<Employee> getBirthday()
{
    var slist = new List<Employee>();
    var db = new BLUEPUMPKINEntities();
    var today = DateTime.Today; //2.
    return (from emp in db.Employees
            let BirthdayDiff = (new DateTime(today.Year, emp.EMP_DOB.Value.Month, emp.EMP_DOB.Value.Day) - today).TotalDays
            let TrueBirthdayDiff = BirthdayDiff >= 0 ? BirthdayDiff : BirthdayDiff + 365 + Convert.ToInt32(DateTime.IsLeapYear(now.Year)) //1, 3 and leap year
            where TrueBirthdayDiff >= 0 && TrueBirthdayDiff <= 15
            select emp).ToList();
}

您正在返回一个空列表...

[WebMethod]
public static IList<Employee> getBirthday()  //<-- changed signature to IList
{
    var slist = new List<Employee>();
    var db = new BLUEPUMPKINEntities();
    var query = from emp in db.Employees
                let BirthdayDiff = (new DateTime(DateTime.Now.Year,     
                emp.EMP_DOB.Value.Month, emp.EMP_DOB.Value.Day) - DateTime.Now).TotalDays
                where BirthdayDiff >= 0 && BirthdayDiff <= 15
                select emp;
    //return slist;  //<-- problem right here!
    return query.ToList();  //<-- this should fix it...!
}

如果你的心和灵魂真的与那个 List<T> 联系在一起,那么就在 return slist 之前这样做:

slist.AddRange(query);

HTH!

通过使用以下查询,我找到了一种了解即将到来的生日的方法。我不知道为什么人们不赞成我的这个 post。

var day = DateTime.Now.Day;
var month = DateTime.Now.Month;
            var query = (from emp in db.Employees
                     where emp.EMP_DOB.HasValue == true
                     && emp.EMP_DOB.Value.Day >= day && emp.EMP_DOB.Value.Month == month
                     select new Birthday
                     {
                         Name = emp.EMP_USERNAME,
                         date = emp.EMP_DOB.Value
                     }).ToList();

        return query;