【发布时间】:2021-10-28 14:31:27
【问题描述】:
我有一个包含三列 ID、Date、Expenses 的表,我试图在 Date 列中搜索一个 int(Date 列中的数据类型是 DateTime)。例如,如果年份是 1998,输入是 8,则应该显示整行。
这是我的代码:
public ActionResult Get(string searchString)
{
DateTime? startDate = new DateTime(2016, 1, 1);
DateTime? endDate = new DateTime(2018, 12, 5);
AccountingEntities db = new AccountingEntities();
var expensesValues = from s in db.Expenses
select s;
if (startDate.HasValue)
{
expensesValues = expensesValues.Where(s => s.Date > startDate && s.Date < endDate);
//This line gives error, I need to convert the s.Date to string so I can use Contains
expensesValues = expensesValues.Where(s => s.Date.Contains(searchString));
}
}
【问题讨论】:
-
给出更多成功和不成功匹配的例子...
-
您可能会发现在代码中将日期保留为 .NET
DateTime,然后在页面上呈现时指定格式可能会更好。DateTime是第一类类型(例如。如果你减去其中两个,你会得到一个TimeSpan代表两个瞬间之间的时间段 -
老实说,我认为你最好放弃这个要求,转而要求用户提供完整的信息,例如从和到的范围,然后留在那里。对我来说,搜索“8”的日期是没有意义的。似乎唯一有用的时间是:“您在什么日期进行交易?” ...“呃。我不记得了。但我认为它在某个地方的日期有 8”。
-
答案更可能是“1998 年”或“1997 年 8 月,因为...”,这就是搜索日期的系统只提供一个范围的原因。你有一个范围,我认为把它留在那个位置而不是“整个 97 年 12 月,然后只有其中有 8 的日期”会很好。..
标签: c# asp.net-mvc linq