API检索c#中按日期时间过滤的数据



我正在尝试从数据库中检索按日期时间筛选的数据。

我正在尝试这样的东西:

[HttpGet("{datetime}/GetDataByDateTime", Name = "GetDataByDateTime")]
public List<StsStatistic> GetDataByDateTime(DateTime dateTime)
{
List<StsStatistic> statisticList = new List<StsStatistic>();
try
{
using (Context db = new Context())
{
statisticList = db.StsStatistics.Where(x => x.StsDatetimeSent.Value.ToString("yyyy-MM-dd HH:mm:ss") == dateTime).ToList();
}
}
catch (Exception ex)
{
string a = ex.Message;
// TODO
// Put logger
}
return statisticList;
}

Swagger给我的信息是:

https://localhost:44310/api/Statistics/{datetime}/GetDataByDateTime

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-45fcb84500917c4e8478fb15fff8a230-650b85b5faaf414d-00",
"errors": {
"dateTime": [
"The value '{datetime}' is not valid."
]
}
}

你能帮忙吗?

谢谢

您的参数dateTime的类型是DateTime-希望列StsDatetimeSent也是-那么为什么在比较之前要将其转换为字符串??

using (fassi_ioc20Context db = new fassi_ioc20Context())
{
statisticList = db.StsStatistics
.Where(x => x.StsDatetimeSent.Value == dateTime)
.ToList();
}

如果都属于DateTime-类型,则保持这种状态并直接比较日期-时间值!停止总是转换每个日期&是时候无缘无故地串了。。。。。。

最新更新