我在数据库中有一个treatmentDate列,其中已提交治疗患者的日期。如果我需要生成特定公司的总和报告,请说在2015年1月1日至2015年1月30日之间,如何构造查询。
以下是我做过的事情,但有错误
var treatmentSum = (from s in db.Treatments
where (s.CompanyId == CompanyID)
s.TreatmentDate >= fromDate && s.TreatmentDate <= toDate
select s.Amount).Sum();
ViewBag.TreatmentSum = treatmentSum;
这是我要搜索的治疗日期列。
您的指导将不胜感激。
您没有发布错误消息。您尝试使用<=和> =比较字符串,而是尝试将所有的Date和Time变量作为DateTime属性处理。这是LINQ表达式的一段代码:
DateTime FromDate= Convert.ToDateTime("04/20/2015");
DateTime ToDate= Convert.ToDateTime("04/28/2015");
ViewBag.TreatmentSum = GetAllTreatments.
Where(c=>Convert.ToDateTime(c.TreatmentDate)>=FromDate &&
Convert.ToDateTime(c.TreatmentDate)<=ToDate).
Sum(c=>c.Amount);
另外,这是针对您特定问题的演示的链接:https : //dotnetfiddle.net/1a50HI
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句