Created
January 17, 2025 17:20
-
-
Save swaters86/a2cf7f2104ef4671d52909e98541c628 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void FilterDate(FilterDescriptor filter, IQueryable<LogWithDetail> query, string columnName) | |
{ | |
if (!string.IsNullOrEmpty(columnName) && filter.FilterValue != null) | |
{ | |
// Get the property info dynamically from the column name | |
var propertyInfo = typeof(LogWithDetail).GetProperty(columnName); | |
if (propertyInfo != null && propertyInfo.PropertyType == typeof(DateTime)) | |
{ | |
// Parse the filter value to DateTime | |
DateTime filterDate = Convert.ToDateTime(filter.FilterValue); | |
// Create a parameter for the lambda expression (e.g., "x") | |
var parameter = Expression.Parameter(typeof(LogWithDetail), "x"); | |
// Dynamically access the property (e.g., "x.CreatedDate", "x.LastDate") | |
var propertyAccess = Expression.Property(parameter, propertyInfo.Name); | |
// Create a constant for the filter value (e.g., "filterDate") | |
var constant = Expression.Constant(filterDate, typeof(DateTime)); | |
Expression comparison = null; | |
// Apply the correct comparison based on the filter operator | |
switch (filter.FilterOperator) | |
{ | |
case Radzen.FilterOperator.Equals: | |
comparison = Expression.Equal(propertyAccess, constant); | |
break; | |
case Radzen.FilterOperator.NotEquals: | |
comparison = Expression.NotEqual(propertyAccess, constant); | |
break; | |
case Radzen.FilterOperator.GreaterThan: | |
comparison = Expression.GreaterThan(propertyAccess, constant); | |
break; | |
case Radzen.FilterOperator.GreaterThanOrEquals: | |
comparison = Expression.GreaterThanOrEqual(propertyAccess, constant); | |
break; | |
case Radzen.FilterOperator.LessThan: | |
comparison = Expression.LessThan(propertyAccess, constant); | |
break; | |
case Radzen.FilterOperator.LessThanOrEquals: | |
comparison = Expression.LessThanOrEqual(propertyAccess, constant); | |
break; | |
default: | |
throw new NotSupportedException($"Unsupported operator: {filter.FilterOperator}"); | |
} | |
// Combine the comparison into a lambda expression (e.g., "x => x.CreatedDate > filterDate") | |
var lambda = Expression.Lambda<Func<LogWithDetail, bool>>(comparison, parameter); | |
// Apply the filter to the query | |
query = query.Where(lambda); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment