C# Linq与实体生日比较

C# Linq与实体生日比较,c#,linq,C#,Linq,我需要使用Linq to实体创建一个查询,其中生日必须在2天前和接下来的30天内 以下内容不返回任何内容: DateTime twoDaysAgo = DateTime.Now.AddDays(-2); int twoDaysAgoDay = twoDaysAgo.Day; int twoDaysAgoMonth = twoDaysAgo.Month; DateTime MonthAway = DateTime.Now.AddDays(30); int monthAwayDay = MonthA

我需要使用Linq to实体创建一个查询,其中生日必须在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;

我收到一个错误,即Linq to实体不支持此操作。感谢您的帮助。

如果您想忽略年值,使用
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;
var bdays=从p开始,单位为db.staff
其中EntityFunctions.DayOfYear(p.BirthDate)>EntityFunctions.DayOfYear(twoDaysAgo)&&
EntityFunctions.DayOfYear(p.生日)
独立解决方案:

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

您可以将所有年份更改为现在,因为年份不相关,然后您可以通过这种方式检查它

        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;
当今天是新的日期时间(2013年12月31日)


如果你把从生日到今天的年数加起来怎么样?
比如: (未经测试)

var now=DateTime.now;
var twoDaysAgo=now.AddDays(-2);
var monthAway=now.now.AddDays(30)
var b天=
来自db.Staff中的p
设bDay=EntityFunctions.AddYears(p.生日,
EntityFunctions.DiffYears(现在,p.生日))
哪里
B天>两天萨戈和
B日<蒙撒韦
按p.生日排序
选择p;

这是否适用于年度边界?ie在12月31日返回生日为1月1日的人是否正确?我想一个简单的解决方法是将
&
替换为
|
如果
twoDaysAgo.DayOfYear>MonthAway.DayOfYear
,要获取12月31日之后或2月1日之前的日期,如果
生日
在闰年,则此操作可能不起作用。将其更改为“或”逻辑,您将返回所有内容,因为您要说的内容相当于“明天之前或昨天之后的所有内容”,即所有内容。遗憾的是,由于年份界限、闰年等原因,这类问题非常棘手。我应该补充一点,我自己也不知道一个特别简洁的解决方案,我只知道其中的陷阱(
EntityFunctions.DayOfYear
不存在。这是否适用于年份边界?即12月31日,它是否会正确返回生日为1月1日的人?@Chris:查看更新的解决方案。感谢您将此告知我。我认为DayOfYear会在闰年出现问题。我以为OP需要linq解决方案?,它被标记为linq。
DateTime.DayOfYear
是否受linq to实体的支持?
        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
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
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;