I need to filter data with Dynamic LINQ by DateTime field.
I'm try doing this like:
public class MyClass
{
private DateTime datetime_field;
//...
//some other fields
//...
}
public void MyMethod(string where_clause)
{
List<MyClass> myData = new List<MyClass>()
{
//...
//initialization
//...
};
var query = myData.Where(where_clause).ToList();
myDataGridView.DataSource = query;
}
// usage
string value = DateTime.Now.ToString(); // for example
if (DateTime.TryParse(value, out DateTime dt))
MyMethod("datetime_field == DateTime(" + dt.Year + ", " + dt.Month + ", " + dt.Day + ", " + dt.Hour + ", " + dt.Minute + ", " + dt.Second + ")");
But the query return zero records. But if i set compare method >=
or <=
instead ==
, query returns needed records amount.
Maybe I'm doing something wrong?
UPDATE:
I found example here: https://stackoverflow.com/a/26450835/7760805, but I'm still search a better solution.
UPDATE:
Solved by Stef Heyenrath.