Example #1
0
        public static MyODataConfigurates GetConfiguratesFromQueryString(string queryString)
        {
            var configurates = new MyODataConfigurates();

            configurates.PropertiesFilter = GetPropertiesFilter(queryString);

            var orders = MatchesBySelector("orderby", queryString);

            configurates.OrderBy = orders.Count > 0 ? orders[orders.Count - 1].Groups["value"].Value : "";

            var types = MatchesBySelector("ordertype", queryString);

            configurates.IsOrderAsceneding = types.Count > 0
                ? types[types.Count - 1].Groups["value"].Value.ToLower() != "descending"
                : true;

            return(configurates);
        }
Example #2
0
        public static Expression <Func <T, bool> > CompileFilters <T>(MyODataConfigurates config)
        {
            var filters = GetFiltersFromConfigurates <T>(config);

            var firstFilter = filters.FirstOrDefault();

            if (firstFilter == null)
            {
                return(x => true);
            }

            var body  = firstFilter.Body;
            var param = firstFilter.Parameters.ToArray();

            foreach (var nextFilter in filters.Skip(1))
            {
                var nextBody = Expression.Invoke(nextFilter, param);
                body = Expression.OrElse(body, nextBody);
            }

            return(Expression.Lambda <Func <T, bool> >(body, param));
        }
Example #3
0
        public static IEnumerable <T> OrderBy <T>(IEnumerable <T> items, MyODataConfigurates config)
        {
            var result = items;

            if (string.IsNullOrEmpty(config.OrderBy))
            {
                return(items);
            }

            if (config.IsOrderAsceneding)
            {
                result =
                    result.OrderBy(
                        item => item.GetValueFromPublicProperyWithIgnoreCase(config.OrderBy));
            }
            else
            {
                result =
                    result.OrderByDescending(
                        item => item.GetValueFromPublicProperyWithIgnoreCase(config.OrderBy));
            }

            return(result);
        }
Example #4
0
        public static IEnumerable <T> ApplyMyODataCongigurates <T>(IEnumerable <T> items, MyODataConfigurates config)
        {
            foreach (var prop in config.PropertiesFilter)
            {
                items =
                    items.Where(
                        item =>
                        ((string)item.GetType().GetTypeInfo().GetProperty(prop.Name).GetValue(item)).IndexOf(
                            prop.Filter) > -1);
            }

            if (string.IsNullOrEmpty(config.OrderBy))
            {
                return(items);
            }


            if (config.IsOrderAsceneding)
            {
                items =
                    items.OrderBy(
                        item =>
                        item.GetType()
                        .GetTypeInfo()
                        .GetProperty(config.OrderBy,
                                     BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
                        .GetValue(item, null));
            }
            else
            {
                items =
                    items.OrderByDescending(
                        item =>
                        item.GetType()
                        .GetTypeInfo()
                        .GetProperty(config.OrderBy,
                                     BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
                        .GetValue(item, null));
            }

            return(items);
        }
Example #5
0
        private static IEnumerable <Expression <Func <T, bool> > > GetFiltersFromConfigurates <T>(MyODataConfigurates config)
        {
            var filters = new List <Expression <Func <T, bool> > >();

            foreach (var prop in config.PropertiesFilter)
            {
                filters.Add(item => GetFilter(prop, item));
            }

            return(filters);
        }