internal OrderByFilterOperation ConstructOrderByExpression <T>(bool nested = false) where T : class { var type = typeof(T); if (string.IsNullOrWhiteSpace(PropertyName)) { PropertyName = EFCoreSugarPropertyCollection.GetDefaultPropertyName(type); } var command = OrderByDirection == OrderByDirection.Descending ? nested ? "ThenByDescending" : "OrderByDescending" : nested ? "ThenBy" : "OrderBy"; var parameter = Expression.Parameter(type, "p"); Expression memberAccess = parameter; foreach (var property in PropertyName.Split('.')) { if (typeof(IEnumerable).IsAssignableFrom(memberAccess.Type)) { var subtype = memberAccess.Type.GetGenericArguments()[0]; memberAccess = Expression.Call( typeof(Enumerable), "FirstOrDefault", new Type[] { subtype }, memberAccess); } memberAccess = Expression.Property(memberAccess, property); } return(new OrderByFilterOperation(PropertyName, command, memberAccess.Type, Expression.Lambda(memberAccess, parameter))); }
public static FilteredQuery <T> Filter <T>(this FilteredQuery <T> baseQuery, Filter filter) where T : class { //we will preserve the first filter paging settings always var newFilteredQuery = filter.ApplyFilter(baseQuery.Query); baseQuery.Query = newFilteredQuery.Query; if (baseQuery.OrderByProperties.Count == 0) { baseQuery.OrderByProperties.Add(baseQuery.OrderBys.First().PropertyName ?? EFCoreSugarPropertyCollection.GetDefaultPropertyName(typeof(T))); } //we want to ignore additional orderbys if they are not set explicitly, this likely means they are doing something else and dont care about the order by var newOrderBy = newFilteredQuery.OrderBys.First(); if (newOrderBy.PropertyName != null && !baseQuery.OrderByProperties.Contains(newOrderBy.PropertyName)) { baseQuery.OrderBys.Add(newOrderBy); baseQuery.OrderByProperties.Add(newOrderBy.PropertyName); } return(baseQuery); }