LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)

后端 未结 3 1819
北荒
北荒 2021-01-21 03:41

The following throws an error:

public FieldViewer GetFieldViewByFieldIDIPAndUserByDate( int fieldID, string ip, string userID, DateTime date )
{
    return this.         


        
3条回答
  •  既然无缘
    2021-01-21 04:03

    EntityFunctions.DiffMinutes for your case will be inefficient. You should do this way

    public FieldViewer GetFieldViewByFieldIDIPAndUserByDate( int fieldID, string ip, string userID, DateTime date )
    {
        var tenMinThreshold = date - TimeSpan.FromMinutes(10);
        return this.context.FieldViewers.Where( x =>
            x.Field.FieldID == fieldID &&
            x.Viewer.IPAddress == ip &&
            x.Viewer.User.Id == userID &&
            x.Viewer.CreatedAt <= tenMinThreshold).FirstOrDefault();
    }
    

提交回复
热议问题