how to create Linq Expression for DateTime.Date
For example,
var query = CollectionQuery.OrderBy(c => c.CreatedAtUtc.Date); // where CollectionQuerry is User type
var expressions = query.Expression;
when I debug I found expressions contain Expression type Quote
I want it to be dynamic so I use System.Linq.Expression (ef core 2.2)
ParameterExpression arg = Expression.Parameter(typeof(User), "c");
var memberExpression = Expression.Property(arg, "CreatedAtUtc");
var quoteExpression = Expression.Quote(memberExpression); // no parameter to set .Date
my problem is I cannot create Expression with .Date, and how can I create the expression? thank you.
CodePudding user response:
after @Jeremy Lakeman suggest
ParameterExpression arg = Expression.Parameter(typeof(User), "c");
var memberExpression = Expression.Property(Expression.Property(arg, "CreatedAtUtc"), "Date"); // add this line
var labmdaExpression = Expression.Lambda<Func<T, DateTime>>(memberExpression, arg);
var quoteExpression = Expression.Quote(labmdaExpression);
when I look into quoteExpression on debug it will show