我试图在获取记录时比较存储在sql中的一个月的日期,但它会抛出异常。
public partial class LateRequest
{
public int LateRequestID { get; set; }
public Nullable<int> EmpID { get; set; }
public Nullable<int> TeamLeadAppr { get; set; }
public Nullable<int> DeptLeadAppr { get; set; }
public Nullable<System.DateTime> lateDate { get; set; }
}
在我的计算课上我有;
public int getMonthlyLatesApproved(int empId)
{
DateTime today = DateTime.Now;
var getMonth = today.Month
var lateApproved = (from obj in db.LateRequests
where obj.EmpID == empId
&& obj.TeamLeadAppr == 1 && obj.DeptLeadAppr == 1
&& obj.lateDate.Value.ToString("mm") == getMonth.ToString()
select obj).Count();
return lateApproved;
}
这不起作用所以我尝试使用LINQ,如:
return db.LateRequests.Where(x => x.EmpID == empId && x.TeamLeadAppr == 1 && x.DeptLeadAppr == 1 &&
x.lateDate.Value.ToString("mm") == getMonth.ToString()).Count();
此操作失败,我将其读为here并尝试使用AsEnumerable()
如下;
return db.LateRequests.Where(x => x.EmpID == empId && x.TeamLeadAppr == 1 && x.DeptLeadAppr == 1 &&
x.lateDate.Value.ToString("mm") == getMonth.ToString()).AsEnumerable().Count();
我需要了解在检索记录时如何将c-sharp与sql命令混合使用?
答案 0 :(得分:0)
您需要的是SqlFunctions.DatePart方法。
&& SqlFunctions.DatePart("month", obj.lateDate) == getMonth