Reputation: 99
I can’t seem to get any results back when searching a datetime column.
DateCreated field is saved as datetime type, within the mssql database.
// datatable
"data": "DateCreated", "name": "DateCreated",
render: function (data, type, row) {
return moment(data).format('DD-MM-YYYY HH:mm:ss A');
}
// code within controller
searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();
tAccounts = dbcontext.ApproveAccounts.Where(x => x.UserID.ToString().Contains(searchValue) || x.DateCreated.ToString().Contains(searchValue)).ToList();
Any pointers much appreciated.
Joe
Upvotes: 0
Views: 123
Reputation: 99
_ = DateTime.TryParse(searchValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime searchDt);
tAccounts = dbcontext.ApproveAccounts
.Where(x => DbFunctions.TruncateTime(x.DateAccountCreated) == searchDt.Date || x.UserID.ToString().Contains(searchValue)).ToList();
Upvotes: 0
Reputation: 11514
You could attempt to parse the searchValue as a DateTime (adjust as needed per your culture and style). Then compare the Dates
(eliminating time element). This assumes nothing will match DateTime.MinValue
which is what you will get if parsing fails.
_ = DateTime.TryParse(searchValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime searchDt);
tAccounts = dbcontext.ApproveAccounts
.Where(x => x.UserID.ToString().Contains(searchValue) || x.DateCreated.Date == searchDt.Date).ToList();
Upvotes: 1