【发布时间】:2016-05-05 22:34:42
【问题描述】:
用户在url中输入两个参数,即开始日期和结束日期,它们以yyyyMMddhhmm的格式作为字符串输入。我正在尝试获取这些字符串并将它们转换为日期,以便查询我的数据库。
[ResponseType(typeof(Detail))]
public IHttpActionResult GetDetail(string StartDate, string EndDate)
{
DateTime StartDateTime;
DateTime EndDateTime;
StartDateTime = new DateTime();
EndDateTime = new DateTime();
StartDateTime = DateTime.ParseExact(StartDate, "yyyyMMddhhmm", null);
EndDateTime = DateTime.ParseExact(EndDate, "yyyyMMddhhmm", null);
var detail = from a in db.Details where (a.callDate >= StartDateTime && a.callDate <= EndDateTime) select a;
var Response = new DetailResponse() { status = true, calls = detail };
return Ok(response);
}
但是我得到 >= 不能在日期时间和字符串中使用的错误。
编辑: 为了回答其中一个问题,我包括了一个用于显示数据的模型类。
DetailResponse.cs
public class DetailResponse
{
public bool status { get; set; }
public string statusMessage { get; set; }
public IQueryable<Detail> calls { get; set; }
}
【问题讨论】:
-
a.callDate 是什么类型?
-
啊,是我的sql数据库中的字符串。
-
您可以在一个语句中声明和分配您的
DateTimes:DateTime StartDateTime = DateTime.ParseExact(StartDate, "yyyyMMddhhmm", null); -
那你也应该把它解析为日期时间
-
错误很明显——你不能比较两种不同的类型。我意识到现在已经有很多答案了,但你的问题到底是什么?
标签: c# linq datetime asp.net-web-api2