Exemplo n.º 1
0
        public virtual IEnumerable <T> ApplyFilter <T>([NotNull] IEnumerable <T> source, [NotNull] string filterExpression)
        {
            Assert.ArgumentNotNull(source, "source");
            Assert.ArgumentNotNull(filterExpression, "filterExpression");

            SpeakDateTimeExtractor extractor = new SpeakDateTimeExtractor();

            filterExpression = extractor.Extract(filterExpression);

            SpeakFreeTextSearchExtractor <T> freeTextSearchExtractor = new SpeakFreeTextSearchExtractor <T>();

            filterExpression = freeTextSearchExtractor.Extract(filterExpression);

            SpeakExpressionLocalizer localizer = new SpeakExpressionLocalizer();

            filterExpression = localizer.Update(filterExpression);

            IEnumerable <string> references = new[] { Assembly.GetAssembly(typeof(Order)).Location, Assembly.GetAssembly(typeof(Queryable)).Location };
            string code = string.Format(CodeTemplate, typeof(IQueryable <T>).IsAssignableFrom(source.GetType()) ? "System.Linq.IQueryable" : "System.Collections.Generic.IEnumerable", typeof(T).FullName, filterExpression);

            Script script = ScriptFactory.Compile(filterExpression, references, code, "C#");

            Assert.IsTrue(script.IsValid, "Unable to apply the filter. Expression compilation failed.");

            return(script.InvokeDefaultMethod <IEnumerable <T> >(source));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Applies the expression as filter.
        /// </summary>
        /// <typeparam name="T">the type.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="filterExpression">The filter expression.</param>
        /// <returns>object that has been filtered by</returns>
        public virtual object ApplyExpressionAsFilter <T>([NotNull] IQueryable <T> source, [NotNull] string filterExpression)
        {
            Assert.ArgumentNotNull(source, "source");
            Assert.ArgumentNotNull(filterExpression, "filterExpression");

            ICollection <string> references = new HashSet <string>();

            this.PopulateSetWithReferencedAssembliesLocations(typeof(T), references);
            this.PopulateSetWithReferencedAssembliesLocations(typeof(IQueryable), references);

            var code = string.Format(CodeTemplateForFilter, "IQueryable", this.BuildTypeName(typeof(T)), string.Format(this.FixCasing(filterExpression), "source"));

            var script = ScriptFactory.Compile(filterExpression, references, code, "C#");

            Assert.IsTrue(script.IsValid, "Unable to apply the filter. Expression compilation failed.");

            return(script.InvokeDefaultMethod <object>(source));
        }