7

我需要使用 Linq to Entities 创建查询,其中生日必须在 2 天前和接下来的 30 天内。

以下不返回任何内容:

DateTime twoDaysAgo = DateTime.Now.AddDays(-2);
int twoDaysAgoDay = twoDaysAgo.Day;
int twoDaysAgoMonth = twoDaysAgo.Month;
DateTime MonthAway = DateTime.Now.AddDays(30);
int monthAwayDay = MonthAway.Day;
int monthAwayMonth = MonthAway.Month;
var bdays = from p in db.Staffs where EntityFunctions.TruncateTime(p.BirthDate) > EntityFunctions.TruncateTime(twoDaysAgo) &&
                     EntityFunctions.TruncateTime(p.BirthDate) < EntityFunctions.TruncateTime(MonthAway)
                    orderby p.BirthDate select p;
return bdays;

我遇到的问题是我需要一些东西,如果生日从 11/3 下降到 12/5,它应该返回它。它失败的原因是生日包括年份。但是,当我使用类似的东西时:

p.BirthDate.Value.Month 

我收到 Linq to Entities 不支持此功能的错误。任何援助将不胜感激。

4

4 回答 4

1

年度包装独立解决方案:

void Main()
{
    var birthdays = new List<DateTime>();
    birthdays.Add(new DateTime(2013, 11, 08));
    birthdays.Add(new DateTime(2012, 05, 05));
    birthdays.Add(new DateTime(2014, 05, 05));
    birthdays.Add(new DateTime(2005, 11, 08));
    birthdays.Add(new DateTime(2004, 12, 31));


    foreach(var date in birthdays.Where(x => x.IsWithinRange(twoDaysAgo, MonthAway))){
      Console.WriteLine(date);
    }           
}

public static class Extensions {
    public static bool IsWithinRange(this DateTime @this, DateTime lower, DateTime upper){
        if(lower.DayOfYear > upper.DayOfYear){
            return (@this.DayOfYear > lower.DayOfYear || @this.DayOfYear < upper.DayOfYear);
        } 

        return (@this.DayOfYear > lower.DayOfYear && @this.DayOfYear < upper.DayOfYear);
    }
}

输出与

DateTime twoDaysAgo = DateTime.Now.AddDays(-2);
DateTime MonthAway = DateTime.Now.AddDays(30);

8/11/2013 0:00:00
8/11/2005 0:00:00

输出与

DateTime twoDaysAgo = new DateTime(2012, 12, 25);
DateTime MonthAway = new DateTime(2013, 01, 05);

31/12/2004 0:00:00
于 2013-11-05T16:33:11.930 回答
0

如果你添加nr怎么样?从生日到今天多少年?
类似的东西:(未经测试)

var now        = DateTime.Now;
var twoDaysAgo = now.AddDays(-2); 
var monthAway  = now.Now.AddDays(30)   

var bdays = 
        from p in db.Staffs 
        let bDay = EntityFunctions.AddYears(p.BirthDate,
                     EntityFunctions.DiffYears(now, p.BirthDate))
        where 
           bDay > twoDaysAgo && 
           bDay < monthAway
        orderby p.BirthDate 
        select p;
于 2013-11-05T17:24:19.373 回答
0

如果要忽略年份的值,那​​么使用DayOfYearfunction 呢?

var bdays = from p in db.Staffs 
            where EntityFunctions.DayOfYear(p.BirthDate) > EntityFunctions.DayOfYear(twoDaysAgo) &&
                  EntityFunctions.DayOfYear(p.BirthDate) < EntityFunctions.DayOfYear(MonthAway)
            orderby p.BirthDate select p;
于 2013-11-05T16:29:43.207 回答
0

您可以将所有年份更改为现在,因为年份无关紧要,然后您可以通过这种方式进行检查

        DateTime twoDaysAgo = DateTime.Today.AddDays(-2);
        DateTime monthAway = DateTime.Today.AddMonths(1);
        List<DateTime> checkDates = new List<DateTime>
        { new DateTime(2011, 11, 3), new DateTime(2011, 12, 5), new DateTime(2011, 12, 6), new DateTime(2011, 11, 2) };
        checkDates = checkDates.Select(x => new DateTime(DateTime.Today.Year, x.Month, x.Day)).ToList();
        var bdays = from p in checkDates
                    where (p >= twoDaysAgo && p <= monthAway) ||
                          (p>= twoDaysAgo.AddYears(-1) && p <= monthAway.AddYears(-1))
                    orderby p
                    select p;

这导致

11/3/2013 12:00:00 AM
12/5/2013 12:00:00 AM

这也适用于今天的以下日期列表new DateTime(2013, 12, 31)

List<DateTime> checkDates = new List<DateTime>
            { new DateTime(2011, 12, 29), new DateTime(2011, 12, 28), new DateTime(2011, 1, 30), new DateTime(2011, 2, 2) };

给出结果

1/30/2013 12:00:00 AM
12/29/2013 12:00:00 AM
于 2013-11-05T16:43:22.063 回答