public static void Validate(HandleNullPropagationOption value, string parameterValue) { if (!IsDefined(value)) { throw Error.InvalidEnumArgument(parameterValue, (int)value, typeof(HandleNullPropagationOption)); } }
public static bool IsDefined(HandleNullPropagationOption value) { if ((value != HandleNullPropagationOption.Default) && (value != HandleNullPropagationOption.True)) { return (value == HandleNullPropagationOption.False); } return true; }
public static Expression <Func <T, bool> > ToFilterExpression <T>(this ODataQueryOptions <T> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default, TimeZoneInfo timeZone = null) { if (options is null || options.Filter is null && options.Search is null) { return(null); } var parameter = Expression.Parameter(typeof(T), "$it"); Expression filterExpression = null; if (options.Filter is not null) { var raw = options.Filter.ToFilterExpression <T>(handleNullPropagation, timeZone); filterExpression = raw.Body.ReplaceParameter(raw.Parameters[0], parameter); } Expression searchExpression = null; if (options.Search is not null) { var raw = options.Search.ToSearchExpression <T>(handleNullPropagation, timeZone); searchExpression = raw.Body.ReplaceParameter(raw.Parameters[0], parameter); } Expression finalExpression = null; if (filterExpression is not null && searchExpression is not null) { finalExpression = Expression.AndAlso(searchExpression, filterExpression); } finalExpression ??= filterExpression ?? searchExpression; return(Expression.Lambda <Func <T, bool> >(finalExpression, parameter)); }
/// <summary> /// Returns a lambda expresion representing the filter /// </summary> /// <typeparam name="T"></typeparam> /// <param name="filterOption"></param> /// <returns></returns> public static Expression <Func <T, bool> > ToFilterExpression <T>(this FilterQueryOption filterOption, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default) { if (filterOption == null) { return(null); } IQueryable queryable = Enumerable.Empty <T>().AsQueryable(); queryable = filterOption.ApplyTo(queryable, new ODataQuerySettings() { HandleNullPropagation = handleNullPropagation }); MethodCallExpression whereMethodCallExpression = (MethodCallExpression)queryable.Expression; return((Expression <Func <T, bool> >)(whereMethodCallExpression.Arguments[1].Unquote() as LambdaExpression)); }
public static bool IsDefined(HandleNullPropagationOption value) { return value == HandleNullPropagationOption.Default || value == HandleNullPropagationOption.True || value == HandleNullPropagationOption.False; }
public static bool IsDefined(HandleNullPropagationOption value) { return(value == HandleNullPropagationOption.Default || value == HandleNullPropagationOption.True || value == HandleNullPropagationOption.False); }
public static async Task <IQueryable <TModel> > GetQueryAsync <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default) where TModel : class { ICollection <Expression <Func <IQueryable <TModel>, IIncludableQueryable <TModel, object> > > > includeExpressions = options.SelectExpand.GetIncludes().BuildIncludesExpressionCollection <TModel>()?.ToList(); Expression <Func <TModel, bool> > filter = options.Filter.ToFilterExpression <TModel>(handleNullPropagation); Expression <Func <IQueryable <TModel>, IQueryable <TModel> > > queryableExpression = options.GetQueryableExpression(); return(await query.GetQueryAsync(mapper, filter, queryableExpression, includeExpressions)); }
public static async Task <ICollection <TModel> > GetAsync <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False) where TModel : class { Expression <Func <TModel, bool> > filter = options.Filter.ToFilterExpression <TModel>(handleNullPropagation); Expression <Func <IQueryable <TModel>, IQueryable <TModel> > > queryableExpression = options.GetQueryableExpression(); Expression <Func <IQueryable <TModel>, long> > countExpression = LinqExtensions.GetCountExpression <TModel>(filter); options.AddExpandOptionsResult(); if (options.Count?.Value == true) { options.AddCountOptionsResult <TModel, TData>(await query.QueryAsync(mapper, countExpression)); } return(await query.GetAsync(mapper, filter, queryableExpression)); }
public NHQueryHelper(bool enableConstantParameterization, bool ensureStableOrdering, HandleNullPropagationOption handleNullPropagation, int pageSize) : base(enableConstantParameterization, ensureStableOrdering, handleNullPropagation, pageSize) { }
public void CreateNamedPropertyExpression_WithDerivedProperty_ReturnsMemberAccessExpression(HandleNullPropagationOption options) { SelectExpandBinder binder = GetBinder <Customer>(_model, options); //Expression customer = Expression.Parameter(typeof(Customer)); output will be different. Expression customer = Expression.Constant(new Customer()); IEdmStructuralProperty vipAddressProperty = _vipCustomer.StructuralProperties().Single(c => c.Name == "VipAddress"); ODataSelectPath selectPath = new ODataSelectPath(new TypeSegment(_vipCustomer, _customer, null), new PropertySegment(vipAddressProperty)); PathSelectItem pathSelectItem = new PathSelectItem(selectPath); NamedPropertyExpression namedProperty = binder.CreateNamedPropertyExpression(customer, _customer, pathSelectItem); Assert.NotNull(namedProperty); Assert.Equal("\"VipAddress\"", namedProperty.Name.ToString()); if (options != HandleNullPropagationOption.True) { Assert.Equal("(value(NS.Customer) As VipCustomer).VipAddress", namedProperty.Value.ToString()); } else { Assert.Equal("IIF(((value(NS.Customer) As VipCustomer) == null)," + " null," + " (value(NS.Customer) As VipCustomer).VipAddress)", namedProperty.Value.ToString()); } }
public void CreateNamedPropertyExpression_NonDerivedProperty_ReturnsMemberAccessExpression(HandleNullPropagationOption options) { SelectExpandBinder binder = GetBinder <Customer>(_model, options); Expression customer = Expression.Constant(new Customer()); IEdmStructuralProperty homeAddressProperty = _customer.StructuralProperties().Single(c => c.Name == "HomeLocation"); ODataSelectPath selectPath = new ODataSelectPath(new PropertySegment(homeAddressProperty)); PathSelectItem pathSelectItem = new PathSelectItem(selectPath); NamedPropertyExpression namedProperty = binder.CreateNamedPropertyExpression(customer, _customer, pathSelectItem); Assert.NotNull(namedProperty); Assert.Equal("\"HomeLocation\"", namedProperty.Name.ToString()); if (options != HandleNullPropagationOption.True) { Assert.Equal("value(NS.Customer).HomeLocation", namedProperty.Value.ToString()); } else { Assert.Equal("IIF((value(NS.Customer) == null)," + " null," + " value(NS.Customer).HomeLocation)", namedProperty.Value.ToString()); } }
public void CreateNamedPropertyExpression_WithMultiplePropertyAndType_ReturnsMemberAccessExpression(HandleNullPropagationOption options) { SelectExpandBinder binder = GetBinder <Customer>(_model, options); Expression customer = Expression.Parameter(typeof(Customer)); // output will be different. // Expression customer = Expression.Constant(new Customer()); SelectExpandClause selectExpandClause = ParseSelectExpand("HomeLocation/NS.CnAddress/Street", null); PathSelectItem pathSelectItem = selectExpandClause.SelectedItems.First() as PathSelectItem; NamedPropertyExpression namedProperty = binder.CreateNamedPropertyExpression(customer, _customer, pathSelectItem); Assert.NotNull(namedProperty); /* * Assert.NotNull(namedProperty); * Assert.Equal("\"VipAddress\"", namedProperty.Name.ToString()); * * if (options != HandleNullPropagationOption.True) * { * Assert.Equal("(value(NS.Customer) As VipCustomer).VipAddress", namedProperty.Value.ToString()); * } * else * { * Assert.Equal("IIF(((value(NS.Customer) As VipCustomer) == null)," + * " null," + * " (value(NS.Customer) As VipCustomer).VipAddress)", namedProperty.Value.ToString()); * }*/ }
public static async Task <IQueryable <TModel> > GetQueryAsync <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False) where TModel : class => await query.GetQueryAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });
public static ICollection <TModel> Get <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False) where TModel : class => query.Get(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });
public QueryHelper(bool enableConstantParameterization, bool ensureStableOrdering, HandleNullPropagationOption handleNullPropagation, int?pageSize) { this.querySettings = NewODataQuerySettings(enableConstantParameterization, ensureStableOrdering, handleNullPropagation, pageSize); }
public static ODataQuerySettings NewODataQuerySettings(bool enableConstantParameterization, bool ensureStableOrdering, HandleNullPropagationOption handleNullPropagation, int?pageSize) { var settings = new ODataQuerySettings() { EnableConstantParameterization = enableConstantParameterization, EnsureStableOrdering = ensureStableOrdering, HandleNullPropagation = handleNullPropagation, PageSize = pageSize > 0 ? pageSize : null }; return(settings); }
public void CreateNamedPropertyExpression_WithMultipleProperty_ReturnsMemberAccessExpression(HandleNullPropagationOption options) { SelectExpandBinder binder = GetBinder <Customer>(_model, options); //Expression customer = Expression.Parameter(typeof(Customer)); output will be different. Expression customer = Expression.Constant(new Customer()); IEdmStructuralProperty homeLocationProperty = _customer.StructuralProperties().Single(c => c.Name == "HomeLocation"); IEdmStructuralProperty streetProperty = _address.StructuralProperties().Single(c => c.Name == "Street"); ODataSelectPath selectPath = new ODataSelectPath(new PropertySegment(homeLocationProperty), new PropertySegment(streetProperty)); PathSelectItem pathSelectItem = new PathSelectItem(selectPath); NamedPropertyExpression namedProperty = binder.CreateNamedPropertyExpression(customer, _customer, pathSelectItem); Assert.NotNull(namedProperty); /* * Assert.NotNull(namedProperty); * Assert.Equal("\"VipAddress\"", namedProperty.Name.ToString()); * * if (options != HandleNullPropagationOption.True) * { * Assert.Equal("(value(NS.Customer) As VipCustomer).VipAddress", namedProperty.Value.ToString()); * } * else * { * Assert.Equal("IIF(((value(NS.Customer) As VipCustomer) == null)," + * " null," + * " (value(NS.Customer) As VipCustomer).VipAddress)", namedProperty.Value.ToString()); * }*/ }
public static async Task <IQueryable <TModel> > GetQueryAsync <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default) where TModel : class { ICollection <Expression <Func <IQueryable <TModel>, IIncludableQueryable <TModel, object> > > > includeExpressions = options.SelectExpand.GetIncludes().BuildIncludesExpressionCollection <TModel>()?.ToList(); Expression <Func <TModel, bool> > filter = options.Filter.ToFilterExpression <TModel>(handleNullPropagation); Expression <Func <IQueryable <TModel>, IQueryable <TModel> > > queryableExpression = options.GetQueryableExpression(); Expression <Func <IQueryable <TModel>, long> > countExpression = LinqExtensions.GetCountExpression <TModel>(filter); options.AddExpandOptionsResult(); if (options.Count?.Value == true) { options.AddCountOptionsResult <TModel, TData>(await query.QueryAsync(mapper, countExpression)); } return(await query.GetQueryAsync(mapper, filter, queryableExpression, includeExpressions)); }
/// <summary> /// Get /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TData"></typeparam> /// <param name="query"></param> /// <param name="mapper"></param> /// <param name="options"></param> /// <returns></returns> public static ICollection <TModel> Get <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default) where TModel : class => Task.Run(async() => await query.GetAsync(mapper, options, handleNullPropagation)).Result;
/// <summary> /// GetQueryAsync /// </summary> /// <typeparam name="TModel"></typeparam> /// <typeparam name="TData"></typeparam> /// <param name="query"></param> /// <param name="mapper"></param> /// <param name="options"></param> /// <returns></returns> public static async Task <IQueryable <TModel> > GetQueryAsync <TModel, TData>(this IQueryable <TData> query, IMapper mapper, ODataQueryOptions <TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default) where TModel : class { var expansions = options.SelectExpand.GetExpansions(typeof(TModel)); List <Expression <Func <TModel, object> > > includeExpressions = expansions.Select(list => new List <Expansion>(list)).BuildIncludes <TModel> ( options.SelectExpand.GetSelects() ) .ToList(); Expression <Func <TModel, bool> > filter = options.Filter.ToFilterExpression <TModel>(handleNullPropagation); Expression <Func <IQueryable <TModel>, IQueryable <TModel> > > queryableExpression = options.GetQueryableExpression(); Expression <Func <IQueryable <TModel>, long> > countExpression = LinqExtensions.GetCountExpression <TModel>(filter); options.AddExpandOptionsResult(); if (options.Count?.Value == true) { options.AddCountOptionsResult <TModel, TData>(await query.QueryAsync(mapper, countExpression)); } IQueryable <TModel> queryable = await query.GetQueryAsync(mapper, filter, queryableExpression, includeExpressions); return(queryable.UpdateQueryableExpression(expansions)); }