Exemple #1
0
        protected IEnumerable <RowItem> Filter(CancellationToken cancellationToken, DataSchema dataSchema, RowFilter filter, PivotedRows pivotedRows)
        {
            if (filter.IsEmptyFilter)
            {
                return(pivotedRows.RowItems);
            }
            var properties = pivotedRows.ItemProperties;

            Predicate <object>[] columnPredicates = new Predicate <object> [properties.Count];

            for (int i = 0; i < properties.Count; i++)
            {
                columnPredicates[i] = filter.GetPredicate(dataSchema, properties[i]);
            }
            var filteredRows = new List <RowItem>();
            // toString on an enum is incredibly slow, so we cache the results in
            // in a dictionary.
            var toStringCaches = new Dictionary <object, string> [properties.Count];

            for (int i = 0; i < properties.Count; i++)
            {
                var property = properties[i];
                if (property.PropertyType.IsEnum)
                {
                    toStringCaches[i] = new Dictionary <object, string>();
                }
            }

            foreach (var row in pivotedRows.RowItems)
            {
                bool matchesText   = string.IsNullOrEmpty(filter.Text);
                bool matchesFilter = true;
                for (int iProperty = 0; iProperty < properties.Count; iProperty++)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    var predicate = columnPredicates[iProperty];
                    if (matchesText && null == predicate)
                    {
                        continue;
                    }
                    var property = properties[iProperty];
                    var value    = property.GetValue(row);
                    if (!matchesText && value != null)
                    {
                        var    cache = toStringCaches[iProperty];
                        string strValue;
                        if (cache == null)
                        {
                            strValue = value.ToString();
                        }
                        else
                        {
                            if (!cache.TryGetValue(value, out strValue))
                            {
                                strValue = value.ToString();
                                cache.Add(value, strValue);
                            }
                        }
                        if (filter.MatchesText(strValue))
                        {
                            matchesText = true;
                        }
                    }
                    matchesFilter = null == predicate || predicate(value);
                    if (!matchesFilter)
                    {
                        break;
                    }
                }
                if (matchesText && matchesFilter)
                {
                    filteredRows.Add(row);
                }
            }

            if (filteredRows.Count == pivotedRows.RowItems.Count)
            {
                return(pivotedRows.RowItems);
            }
            return(filteredRows);
        }