/// <summary> /// Performs the required includes on the specified <paramref name="queryable"/> /// </summary> /// <param name="queryable">The queryable</param> /// <param name="joinMap">The join map</param> /// <returns></returns> private IQueryable PerformThenInclude(IQueryable queryable, JoinMap joinMap) { // Get the ThenInclude<TEntity, TPreviousProperty, TProperty> method var thenIncludeMethod = typeof(EntityFrameworkQueryableExtensions).GetMethods().Where(x => x.Name == nameof(EntityFrameworkQueryableExtensions.ThenInclude)).ElementAt(0); // Get the type of the model var modelType = queryable.GetType().GetGenericArguments()[0]; // Get the previous property type var previousPropertyType = TypeHelpers.GetNonEnumerableType(queryable.GetType().GetGenericArguments()[1]); // Get the navigation property name var navigationPropertyName = CeidDiplomatikiHelpers.GetPluralForm(joinMap.ReferencedTable.TableName); // Get the navigation property var navigationProperty = previousPropertyType.GetProperty(navigationPropertyName); // Create the expression var expression = ExpressionHelpers.CreatePropertySelectorExpression(previousPropertyType, navigationProperty); // Set the generic arguments (TEntity, TPreviousProperty, TProperty) thenIncludeMethod = thenIncludeMethod.MakeGenericMethod(modelType, previousPropertyType, navigationProperty.PropertyType); // Call the ThenInclude method queryable = (IQueryable)thenIncludeMethod.Invoke(null, new object[] { queryable, expression }); // Return the queryable return(queryable); }
/// <summary> /// Uses the <see cref="Queryable.OrderByDescending{TSource, TKey}(IQueryable{TSource}, Expression{Func{TSource, TKey}})"/> method /// to apply an order by condition to the specified <paramref name="queryable"/> /// </summary> /// <param name="queryable"></param> /// <param name="queryableType"></param> /// <param name="propertyInfo"></param> /// <returns></returns> public static IQueryable AddOrderByDescendinCondition(IQueryable queryable, Type queryableType, PropertyInfo propertyInfo) { // Get the Take method var method = typeof(Queryable).GetMethods().First(x => x.Name == nameof(Queryable.OrderByDescending) && x.GetParameters().Count() == 2).MakeGenericMethod(queryableType, propertyInfo.PropertyType); // Create the expression var expression = ExpressionHelpers.CreatePropertySelectorExpression(queryableType, propertyInfo); // Call the method queryable = (IQueryable)method.Invoke(null, new object[] { queryable, expression }); // Return the queryable return(queryable); }
/// <summary> /// Performs the required includes on the specified <paramref name="queryable"/> /// </summary> /// <param name="queryable">The queryable</param> /// <param name="initialTable">The initial table</param> /// <returns></returns> private IQueryable PerformIncludes(IQueryable queryable, IDbProviderTable initialTable) { // For every join... foreach (var join in Joins) { var joinsPath = ComputePath(join); // For every related join... foreach (var j in joinsPath) { // Get the type of the model var modelType = queryable.GetType().GetGenericArguments()[0]; // If it's the first join... if (joinsPath.ElementAt(0) == j) { // Get the navigation property name var navigationPropertyName = CeidDiplomatikiHelpers.GetPluralForm(j.ReferencedTable.TableName); // Get the navigation property var navigationProperty = modelType.GetProperty(navigationPropertyName); // Create the expression var expression = ExpressionHelpers.CreatePropertySelectorExpression(modelType, navigationProperty); // Get the Include<TEntity, TProperty> method var includeMethod = typeof(EntityFrameworkQueryableExtensions).GetMethods().First(x => x.Name == nameof(EntityFrameworkQueryableExtensions.Include) && x.GetParameters()[1].ParameterType != typeof(string)); // Set the generic arguments (TEntity and TProperty) includeMethod = includeMethod.MakeGenericMethod(modelType, navigationProperty.PropertyType); // Call the Include method queryable = (IQueryable)includeMethod.Invoke(null, new object[] { queryable, expression }); // Continue continue; } queryable = PerformThenInclude(queryable, j); } } // Return the queryable return(queryable); }