public void When_filtering_enumeration_should_parse(string query, Numbers[] expectedNumbers)
        {
            var data = new[] {
                new EnumHolder()
                {
                    Number = Numbers.One, NullableNumber = Numbers.One
                },
                new EnumHolder()
                {
                    Number = Numbers.Two, NullableNumber = Numbers.Two
                },
                new EnumHolder()
                {
                    Number = Numbers.Three, NullableNumber = Numbers.Three
                },
                new EnumHolder()
                {
                    Number = Numbers.Five, NullableNumber = Numbers.Five
                },
            }.AsQueryable();

            var filter   = new ODataFilterLanguage().Parse <EnumHolder>(query);
            var filtered = data.Where(filter).ToList();

            var distinct = filtered.Select(x => x.Number).Distinct().ToArray();

            Assert.Equal(expectedNumbers, distinct);
        }
        public void When_arithmetic_should_evaluate(string query)
        {
            var filter = new ODataFilterLanguage().Parse <object>(query);
            var stringParserCompiled = filter.Compile();

            Assert.True(stringParserCompiled(null));
        }
        public void Should_be_faster_than_linqToQuerystring()
        {
            var baseDatetime = new DateTime(2003, 01, 01);

            var linqToQueryStringStopwatch = new Stopwatch();

            linqToQueryStringStopwatch.Start();
            for (int i = 0; i < 10000; i++)
            {
                var date = baseDatetime.AddDays(i).ToString("s");
                var linqToQuerystringFiltered = Data.ConcreteCollection.LinqToQuerystring($"?$filter=Name eq 'Apple' and (Complete eq true or Date gt datetime'{date}')");
            }
            linqToQueryStringStopwatch.Stop();



            var parseStringStopwatch = new Stopwatch();

            parseStringStopwatch.Start();
            var language = new ODataFilterLanguage();

            for (int i = 0; i < 10000; i++)
            {
                var date   = baseDatetime.AddDays(i).ToString("s");
                var filter = language.Parse <LinqToQuerystringTestDataFixture.ConcreteClass>($"Name eq 'Apple' and (Complete eq true or Date gt datetime'{date}')");
            }
            parseStringStopwatch.Stop();

            Output.WriteLine($"LinqToQueryString Duration: {linqToQueryStringStopwatch.Elapsed}");
            Output.WriteLine($"StringToExpression Duration: {parseStringStopwatch.Elapsed}");

            Assert.True(parseStringStopwatch.ElapsedMilliseconds < linqToQueryStringStopwatch.ElapsedMilliseconds);
        }
        public void When_filtering_nullable_enumeration_should_parse(string query, Numbers[] expectedNumbers)
        {
            var data = new[] {
                new EnumHolder()
                {
                    NullableNumber = Numbers.One
                },
                new EnumHolder()
                {
                    NullableNumber = Numbers.Two
                },
                new EnumHolder()
                {
                    NullableNumber = Numbers.Three
                },
                new EnumHolder()
                {
                    NullableNumber = Numbers.Five
                },
                new EnumHolder()
                {
                    NullableNumber = null
                },
            }.AsQueryable();

            var filter   = new ODataFilterLanguage().Parse <EnumHolder>(query);
            var filtered = data.Where(filter).ToList();

            //we will treat 0 as null (limit put on us by attributes)
            var expectedNumbersWithNull = expectedNumbers.Select(x => x == (Numbers)0 ? null : (Numbers?)x);

            var distinct = filtered.Select(x => x.NullableNumber).Distinct().ToArray();

            Assert.Equal(expectedNumbersWithNull, distinct);
        }
Exemplo n.º 5
0
        public void Parse(string queryString)
        {
            if (!string.IsNullOrEmpty(queryString))
            {
                var queryStrings = HttpUtility.ParseQueryString(queryString);
                if (queryStrings.HasKeys() && queryStrings.AllKeys.Contains("$filter"))
                {
                    string filter   = queryStrings["$filter"];
                    var    language = new ODataFilterLanguage();
                    Expression <Func <T, bool> > predicateExpression = language.Parse <T>(filter);
                    Filter = predicateExpression.Compile();
                }
                if (queryStrings.HasKeys() && queryStrings.AllKeys.Contains("$top"))
                {
                    string top = queryStrings["$top"];
                    ParseTop(top);
                }
                if (queryStrings.HasKeys() && queryStrings.AllKeys.Contains("$skip"))
                {
                    string skip = queryStrings["$skip"];
                    ParseSkip(skip);
                }

                if (queryStrings.HasKeys() && queryStrings.AllKeys.Contains("$orderby"))
                {
                    string            orderby       = queryStrings["$orderby"];
                    OrderByClause <T> orderbyClause = new OrderByClause <T>();
                    orderbyClause.Parse(orderby);
                    Sort          = orderbyClause.RootExpression;
                    SortDirection = orderbyClause.Direction;
                }
            }
        }
Exemplo n.º 6
0
        public static Expression <Func <T, bool> > ParseFilter <T>(string filter) where T : class
        {
            var language = new ODataFilterLanguage();

            var formatterFilter = FormatFilter(filter);

            return(language.Parse <T>(formatterFilter));
        }
        public void When_property_paths_results_as_linqToQuerystring(string query)
        {
            var linqToQuerystringFiltered = Data.ComplexCollection.LinqToQuerystring("?$filter=" + query).ToList();

            var filter = new ODataFilterLanguage().Parse <LinqToQuerystringTestDataFixture.ComplexClass>(query);
            var stringParserFiltered = Data.ComplexCollection.Where(filter).ToList();

            Assert.Equal(linqToQuerystringFiltered, stringParserFiltered);
        }
        public void When_functions_return_same_results_as_linqToQuerystring(string query)
        {
            var linqToQuerystringFiltered = Data.FunctionConcreteCollection.LinqToQuerystring("?$filter=" + query).ToList();

            var filter = new ODataFilterLanguage().Parse <LinqToQuerystringTestDataFixture.ConcreteClass>(query);
            var stringParserFiltered = Data.FunctionConcreteCollection.Where(filter).ToList();

            Assert.Equal(linqToQuerystringFiltered, stringParserFiltered);
        }
Exemplo n.º 9
0
        private static IQueryable <T> ApplyFilterInternal <T>(IQueryable <T> query, string oDataFilterString)
        {
            if (string.IsNullOrWhiteSpace(oDataFilterString))
            {
                return(query);
            }

            var filterExpression = new ODataFilterLanguage().Parse <T>(oDataFilterString);

            return(query.Where(filterExpression));
        }
Exemplo n.º 10
0
 private static IQueryable <T> ApplyFilter <T>(this IQueryable <T> pQuery, string pFilter)
 {
     if (String.IsNullOrEmpty(pFilter))
     {
         return(pQuery);
     }
     try
     {
         var xCompiledFilter = new ODataFilterLanguage().Parse <T>(pFilter);
         return(pQuery.Where(xCompiledFilter));
     }
     catch (Exception e)
     {
         throw new FormatException($"Provided filter expression '{pFilter}' has incorrect format", e);
     }
 }
Exemplo n.º 11
0
        public static IQueryable <T> ApplyFilter <T>(this IQueryable <T> query, string filter)
        {
            if (string.IsNullOrEmpty(filter))
            {
                return(query);
            }

            try
            {
                var compiledFilter = new ODataFilterLanguage().Parse <T>(filter);

                return(query.Where(compiledFilter));
            }
            catch (Exception e)
            {
                throw new FormatException($"Provided filter expression '{filter}' has incorrect format", e);
            }
        }
Exemplo n.º 12
0
        public Expression <Func <TEntity, bool> > GetFilterPredicate(string filter)
        {
            Expression <Func <TEntity, bool> > result = null;

            if (!string.IsNullOrWhiteSpace(filter))
            {
                var language = new ODataFilterLanguage();
                try
                {
                    result = language.Parse <TEntity>(filter);
                }
                catch (Exception exception)
                {
                    throw Error.FilterOData(exception);
                }
            }

            return(result);
        }
Exemplo n.º 13
0
        public Expression <Func <WorkOrderClass, bool> > GetFilterPredicate(string filter)
        {
            Expression <Func <WorkOrderClass, bool> > result = null;

            if (!string.IsNullOrWhiteSpace(filter))
            {
                var language = new ODataFilterLanguage();
                try
                {
                    result = language.Parse <WorkOrderClass>(filter);
                }
                catch (Exception exception)
                {
                    throw new FilterODataException(exception);
                }
            }

            return(result);
        }
Exemplo n.º 14
0
        public FilterDefinition <Flow> GetFilterDefinition(string filter)
        {
            var filterDefinition = Builders <Flow> .Filter.Empty;

            try
            {
                if (!string.IsNullOrWhiteSpace(filter))
                {
                    var matches = filter.Split(new string[] { " and " }, StringSplitOptions.RemoveEmptyEntries);

                    if (matches.Count() == 9)
                    {
                        var properties = GetFilterProperties(matches);
                        var source     = (from p in properties
                                          where p.Name == FlowFilterProperties.Source
                                          select p.Value).SingleOrDefault();
                        var operation = (from p in properties
                                         where p.Name == FlowFilterProperties.Operation
                                         select p.Value).SingleOrDefault();
                        var site = (from p in properties
                                    where p.Name == FlowFilterProperties.Site
                                    select p.Value).SingleOrDefault();
                        var operationalDepartment = (from p in properties
                                                     where p.Name == FlowFilterProperties.OperationalDepartment
                                                     select p.Value).SingleOrDefault();
                        var typePlanning = (from p in properties
                                            where p.Name == FlowFilterProperties.TypePlanning
                                            select p.Value).SingleOrDefault();
                        var customer = (from p in properties
                                        where p.Name == FlowFilterProperties.Customer
                                        select p.Value).SingleOrDefault();
                        var productionSite = (from p in properties
                                              where p.Name == FlowFilterProperties.ProductionSite
                                              select p.Value).SingleOrDefault();
                        var transportType = (from p in properties
                                             where p.Name == FlowFilterProperties.TransportType
                                             select p.Value).SingleOrDefault();
                        var driverWait = (from p in properties
                                          where p.Name == FlowFilterProperties.DriverWait
                                          select p.Value).SingleOrDefault();

                        filterDefinition = Builders <Flow> .Filter.Where(x => (x.Filter.Sources.Contains(new FlowSource(source)) || x.Filter.Sources.Contains(new FlowSource("x"))) &&
                                                                         (x.Filter.Operations.Contains(new FlowOperation(operation)) || x.Filter.Operations.Contains(new FlowOperation("x"))) &&
                                                                         (x.Filter.Sites.Contains(new FlowSite(site)) || x.Filter.Sites.Contains(new FlowSite("x"))) &&
                                                                         (x.Filter.OperationalDepartments.Contains(new FlowOperationalDepartment(operationalDepartment)) || x.Filter.OperationalDepartments.Contains(new FlowOperationalDepartment("x"))) &&
                                                                         (x.Filter.TypePlannings.Contains(new FlowTypePlanning(typePlanning)) || x.Filter.TypePlannings.Contains(new FlowTypePlanning("x"))) &&
                                                                         (x.Filter.Customers.Contains(new FlowCustomer(customer)) || x.Filter.Customers.Contains(new FlowCustomer("x"))) &&
                                                                         (x.Filter.ProductionSites.Contains(new FlowProductionSite(productionSite)) || x.Filter.ProductionSites.Contains(new FlowProductionSite("x"))) &&
                                                                         (x.Filter.TransportTypes.Contains(new FlowTransportType(transportType)) || x.Filter.TransportTypes.Contains(new FlowTransportType("x"))) &&
                                                                         (x.Filter.DriverWait == driverWait || x.Filter.DriverWait == "x"));
                    }
                    else
                    {
                        try
                        {
                            var language  = new ODataFilterLanguage();
                            var predicate = language.Parse <Flow>(filter);
                            filterDefinition = predicate;
                        }
                        catch (OperationInvalidException ex)
                        {
                            throw new FilterODataException(ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new FilterODataException(ex.Message);
            }

            return(filterDefinition);
        }