samedi 21 août 2021

Check if DateTime has time or not

Say I have a database column 'A'. The type of column in DB can either be Date or DateTime. I am creating a dynamic filter query:

filterFragment = Expression.AndAlso(
                            Expression.GreaterThanOrEqual(memberExpression, Expression.Constant(beginDate)),
                                Expression.LessThan(memberExpression, Expression.Constant(endDate.AddSeconds(1))));

where,

memberExpression = Expression.Property(rootExpression, property);
rootExpression = Expression.Parameter(targetType, targetType.Name);
queryableSource = source.AsQueryable();
results = queryableSource.Expression;
targetType = queryableSource.ElementType;

and

IEnumerable<TSource> source is a parameter to the method generating the dynamic filterExpression.

Attached a screenshot of memberExpression value during runtime: enter image description here

and here's the Type value:

enter image description here

How do I check if the Type of memberExpression has Time part or it is a ShortDate without any time part? The reason I want to check the type of memberExpression is that if it has Time part I need to do endDate.AddSecond(1), and if there isn't a time part need to use LessThanOrEqual in Expression as:

filterFragment = Expression.AndAlso(
                            Expression.GreaterThanOrEqual(memberExpression, Expression.Constant(beginDate)),
                                Expression.LessThanOrEqual(memberExpression, Expression.Constant(endDate)));

More details of question here.





Aucun commentaire:

Enregistrer un commentaire