我有一个MVC应用程序,因为我想根据我的要求从sql server数据库中获取记录。主要是我有表“OfficeDetail”,其中一列是“ApprovedDate”,第二列是“ChildCount”。 “Approvedate”包含用户文档批准日期。第二个“ChildCount”包含整数值。我还在这个网站上搜索了另一个问题,但没有得到解决方案。我正在使用两个条件,所以为了获取记录我写了这段代码。
public IEnumerable<EmployeeModel> GetAllExpired()
{
DateTime my = DateTime.Today;
DBContext = new ConsumerNomineeFormEntities();
return (from f in DBContext.OfficeDetails
where (SqlFunctions.DateDiff("second",f.Date,my.Date)>90)
where (f.ChildCount>5)
select new EmployeeModel
{
XConsumer_Id = f.ConsumerNo_,
XApproved = f.Date,
XChildCount = f.ChildCount,
XTimeSpent = (f.Date - DateTime.Today).TotalDays
}).ToList();
}
答案 0 :(得分:2)
问题似乎是XTimeSpent的计算,因为你需要使用sqlfunctions来减去日期
答案 1 :(得分:0)
好的,我终于得到了,我必须做出这样的改变,
DBContext = new ConsumerNomineeFormEntities();
return (from f in DBContext.OfficeDetails
where SqlFunctions.DateDiff("day", f.Date, my.Date) > 90
where f.ChildCount < 5
select new EmployeeModel
{
XConsumer_Id = f.ConsumerNo_,
XApproved = f.Date,
XChildCount = f.ChildCount,
XTimeSpent = SqlFunctions.DateDiff("day", f.Date, my.Date)
}).ToList();