二元运算符 Equal 未针对 'System.DateTime' 和 'System.Object' 类型定义。--比较日期时间 linq



我在linq中比较两个日期范围时遇到问题。我的视图抛出了一个异常

没有为类型定义二进制运算符Equal"System.DateTime"one_answers"System.Object"。请告诉我如何解决此问题?如有任何帮助,我们将不胜感激。

 public ViewResult List(SearchTerms search, int page = 1)
 {
        (from posts in repository.JobPosts
                               orderby posts.PostDate descending
                               select new
                               {
                                   Id = posts.Id,     
                                   ......
                               })
      .Where(x =>  ((search.PostedDateFrom.CompareTo(DbFunctions.TruncateTime(DateTime.Now.Date)) != 0
                                  || (search.PostedDateTo.CompareTo(DbFunctions.TruncateTime(DateTime.Now.Date)) != 0)) ?
                                      x.PostDate >= search.PostedDateTo && x.PostDate <= search.PostedDateTo : x.PostDate !=null)
                                      )).....
}

我的搜索术语模型。

 public class SearchTerms
    {
        public string searchText { get; set; }
        public string JobFunction { get; set; }
        public string JobIndustry { get; set; }
        public string jobType { get; set; }
        public string JobLevel { get; set; }
        public DateTime PostedDateFrom { get; set; }
        public DateTime PostedDateTo { get; set; }
        public decimal MinSalary { get; set; }
        public string JobRegion { get; set; }
    }

如有任何帮助,我们将不胜感激。

更新

下面是我的JobPost模型。

 public class JobPost
    {
        public long Id { get; set; }
        public string Post { get; set; }
        public DateTime PostDate { get; set; }
        public long UserId { get; set; }
        public string Region { get; set; }
        public string JobType { get; set; }
        public string PostTitle { get; set; }
        public string Industry { get; set; }
        public string JobFunction { get; set; }
        public string JobLevel { get; set; }
        public decimal Salary { get; set; }
        public int Experience { get; set; }
        public DateTime JobExpiryDate { get; set; }
        public bool Active { get; set; }

    }

我认为您的问题是DateTime.Compare接受DateTime类型的参数,但TruncateTime返回一个可以为null的DateTime。我认为您必须从TruncateTime中投射结果。

相关内容