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

年は無関係なので、すべての年を現在に変更でき、この方法で確認できます

        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 に答える
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 に答える