Exemplo n.º 1
0
        /// <summary>
        /// Returns IQueryable filtered Data.
        /// </summary>
        /// <typeparam name="TFilter">Filter model type</typeparam>
        /// <typeparam name="TList">Data model type</typeparam>
        /// <param name="filterModel">Filter data</param>
        /// <param name="list">Data which should be filtered</param>
        /// <returns>Filtered data</returns>
        public static IQueryable <TList> Filter <TFilter, TList>(TFilter filterModel, IQueryable <TList> list) where TFilter : BaseFilter
        {
            //Filter Model Generator
            var filterGenerator = new FilterModelGenerator <TFilter>();

            filterGenerator.GenerateFilterModel(filterModel);

            //Query Generator

            var queryGenerator = new QueryGenerator <TList>();

            //Filter Parameter
            if (!filterGenerator.Filters.Any())
            {
                return(list);
            }

            var filters = filterGenerator.Filters.GroupBy(f => f.PropertyName);

            foreach (var filter in filters)
            {
                var index = 0;
                foreach (var item in filter)
                {
                    if (item.AlreadyUsed)
                    {
                        continue;
                    }

                    GenerateFilterQuery(queryGenerator, item);

                    var sameFilters = filterGenerator.Filters
                                      .Where(f => f.FilterPropertyName == item.FilterPropertyName && f.PropertyName != item.PropertyName && !f.AlreadyUsed)
                                      .ToList();
                    if (sameFilters.Any())
                    {
                        foreach (var sameItem in sameFilters)
                        {
                            GenerateFilterQuery(queryGenerator, sameItem);

                            if (!sameItem.ConditionalOperator.HasValue)
                            {
                                throw new Exception($"{nameof(item.FilterPropertyName)} does not have ConditionalOperator for filtering {item.PropertyName}"); // Add Custom Exception
                            }
                            queryGenerator.Condition(sameItem.ConditionalOperator.Value);
                        }
                    }

                    if (filter.Count() != 1 && index > 0 && item.ConditionalOperator.HasValue)
                    {
                        queryGenerator.Condition(item.ConditionalOperator.Value);
                    }
                    index++;
                }

                queryGenerator.AddFilter();
            }

            return(queryGenerator.ApplyFilter(list));
        }
        private async Task <Solution> GenerateModelFiltersAsync(
            Solution solution,
            ProjectId projectId,
            string namespaceName,
            string className,
            IEnumerable <PropertyInfo> properties,
            bool previewMode,
            CancellationToken cancellationToken)
        {
            properties = properties.ToList();
            if (!previewMode)
            {
                bool result = false;
                ThreadHelper.Generic.Invoke(() =>
                {
                    var dialog = new SelectPropertiesDialog(properties);
                    result     = dialog.ShowModal() ?? false;
                });

                if (!result)
                {
                    return(solution);
                }

                properties = properties.Where(p => p.Included);
            }

            var filterModelClass      = FilterModelGenerator.FilterModelClass(className, properties);
            var filterExtensionsClass = FilterExtensionsGenerator.FilterExtensionsClass(className, properties);
            var filtersRoot           = BaseSyntaxGenerator.CompilationUnit(namespaceName, filterModelClass, filterExtensionsClass) as SyntaxNode;
            var documentId            = DocumentId.CreateNewId(projectId);

            solution = solution.AddDocument(documentId, className + "Filters", filtersRoot);

            var newDoc = solution.GetDocument(documentId);

            var options = solution.Workspace.Options
                          .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, 3)
                          .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, true)
                          .WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, Environment.NewLine)
                          .WithChangedOption(FormattingOptions.SmartIndent, LanguageNames.CSharp, FormattingOptions.IndentStyle.Smart);

            newDoc = await Simplifier.ReduceAsync(newDoc, Simplifier.Annotation, cancellationToken : cancellationToken).ConfigureAwait(false);

            newDoc = await Formatter.FormatAsync(newDoc, Formatter.Annotation, options, cancellationToken).ConfigureAwait(false);

            var formattedRoot = await newDoc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            if (!previewMode)
            {
                ThreadHelper.Generic.BeginInvoke(() =>
                {
                    solution.Workspace.OpenDocument(documentId);
                });
            }

            return(solution.WithDocumentSyntaxRoot(documentId, formattedRoot));
        }