【发布时间】:2017-12-11 16:51:30
【问题描述】:
我正在尝试在可选日期字段上过滤 Linq to SQL 查询,但遇到了问题。方法如下:
public List<ReferralTrackerModel> GetDoctorDailySchedule(int DBID, int DoctorCode, string apptDateFrom, string apptDateTo)
{
using (var dbIMEC = getISSDataContext(DBID))
{
var eResults = from c in dbIMEC.tblCases
join s in dbIMEC.tblServices on c.ServiceCode equals s.ServiceCode
join e in dbIMEC.tblExaminees on c.ChartNbr equals e.ChartNbr
join p in dbIMEC.tblPublishOnWebs on c.CaseNbr equals p.TableKey
join ct in dbIMEC.tblCaseTypes on c.CaseType equals ct.Code
join l in dbIMEC.tblLocations on c.DoctorLocation equals l.LocationCode.ToString()
where p.UserCode == DoctorCode
where p.TableType == "tblCase"
where p.PublishOnWeb
where p.TableKey == c.CaseNbr
where p.UserType == "DR"
//where c.ApptDate >= DateTime.Parse(apptDateFrom)
//where c.ApptDate <= DateTime.Parse(apptDateFrom).Date.AddHours(23).AddMinutes(59).AddSeconds(59)
select new ReferralTrackerModel()
{
CaseNbr = c.CaseNbr,
ClaimNbr = c.ClaimNbr.Trim(),
ExamLocationName = l.Location.Trim(),
ExamineeName = e.LastName.Trim() + ", " + e.FirstName.Trim(),
CaseType = ct.Description.Trim(),
ServiceDesc = s.Description.Trim(),
ApptDateTime = c.ApptTime.ToString().Trim(),
ApptDateTimeDate = DateTime.Parse(c.ApptTime.ToString())
};
if (apptDateTo.Length > 0)
{
var dateTo = DateTime.Parse(apptDateTo);
eResults = eResults.Where(r => r.ApptDateTimeDate <= dateTo);
}
var lst = eResults.ToList();
return lst;
}
}
The date calculation in the main part of the query is working as expected but the portion after the if statement is not. I am getting the following error back from the API controller:
“消息”:“方法 'System.DateTime Parse(System.String)' 没有支持的 SQL 转换。”
我的目的是在存在可选的 apptDateTo 字段时过滤结果。
【问题讨论】:
标签: c# sql linq asp.net-web-api