我正在和linq中发布比较两个日期范围。我的观点引发了异常
没有为类型定义二元运算符Equal 'System.DateTime'和'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; }
}
答案 0 :(得分:0)
我认为你的问题是DateTime.Compare采用DateTIme类型的参数,但TruncateTime返回一个可为空的DateTime。我认为你必须从TruncateTime转换结果。