Linq与实体生日比较

时间:2013-11-05 16:22:53

标签: c# linq

我需要使用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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果你想忽略年份的价值,那么使用DayOfYear函数呢?

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;

答案 2 :(得分: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

答案 3 :(得分: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;