31

我试图使不到 30 天前回复的友谊请求无效。

var requestIgnored = context.Request
    .Where(c => c.IdRequest == result.IdRequest 
             && c.IdRequestTypes == 1 
             && c.Accepted == false 
             && DateTime.Now <= (((DateTime)c.DateResponse).AddDays(30)))
   .SingleOrDefault();

c.DateResponse是类型DateTime?。我遇到的错误是:

LINQ 无法识别命令 .AddDays

4

2 回答 2

58

编辑:如果您使用的是 EntityFramework >= 6.0,请使用DbFunctions.AddDays. 对于旧版本的实体框架,使用EntityFunctions.AddDays

var requestIgnored = context.Request
    .Where(c => c.IdRequest == result.IdRequest 
             && c.IdRequestTypes == 1 
             && c.Accepted == false 
             && DateTime.Now <= DbFunctions.AddDays(c.DateResponse, 30))
   .SingleOrDefault();
于 2013-06-28T19:08:10.697 回答
6

你可以试试这个:

var thirtyDaysAgo = DateTime.Now.AddDays(-30);
var requestIgnored = context.Request
    .Where(c => 
        c.IdRequest == result.IdRequest && 
        c.IdRequestTypes == 1 && 
        c.Accepted == false && 
        c.DateResponse.HasValue &&
        thirtyDaysAgo <= c.DateResponse.Value)
    .SingleOrDefault();
于 2013-06-28T19:04:30.180 回答