Esempio n. 1
0
        internal SingleFilterInfo ParseFilter(string filterPart)
        {
            SingleFilterInfo filterInfo = new SingleFilterInfo();

            filterInfo.OperatorValue = DetermineFilterOperator(filterPart);

            string[] filterStringParts =
                filterPart.Split(new char[] { (char)filterInfo.OperatorValue });

            filterInfo.PropName = filterStringParts[0].Replace("[", "").Replace("]", "").Replace(" AND ", "").Trim();

            // Get the property descriptor for the filter property name.
            PropertyDescriptor filterPropDesc = TypeDescriptor.GetProperties(typeof(T))[filterInfo.PropName];

            // Convert the filter compare value to the property type.
            if (filterPropDesc == null)
            {
                throw new InvalidOperationException("Specified property to " + "filter " + filterInfo.PropName + " on does not exist on type: " + typeof(T).Name);
            }

            filterInfo.PropDesc = filterPropDesc;

            string comparePartNoQuotes = StripOffQuotes(filterStringParts[1]);

            try
            {
                TypeConverter converter = TypeDescriptor.GetConverter(filterPropDesc.PropertyType);
                filterInfo.CompareValue = converter.ConvertFromString(comparePartNoQuotes);
            }
            catch (NotSupportedException)
            {
                throw new InvalidOperationException("Specified filter" + "value " + comparePartNoQuotes + " can not be converted" + "from string. Implement a type converter for " + filterPropDesc.PropertyType.ToString());
            }
            return(filterInfo);
        }
Esempio n. 2
0
        internal void ApplyFilter(SingleFilterInfo filterParts)
        {
            List <T> results;

            // Check to see if the property type we are filtering by implements
            // the IComparable interface.
            Type interfaceType = TypeDescriptor.GetProperties(typeof(T))[filterParts.PropName].PropertyType.GetInterface("IComparable");

            if (interfaceType == null)
            {
                throw new InvalidOperationException("Filtered property" + " must implement IComparable.");
            }

            results = new List <T>();

            // Check each value and add to the results list.
            foreach (T item in this)
            {
                if (filterParts.PropDesc.GetValue(item) != null)
                {
                    IComparable compareValue = filterParts.PropDesc.GetValue(item) as IComparable;
                    int         result       = compareValue.CompareTo(filterParts.CompareValue);

                    if (filterParts.OperatorValue == FilterOperator.EqualTo && result == 0)
                    {
                        results.Add(item);
                    }

                    if (filterParts.OperatorValue == FilterOperator.GreaterThan && result > 0)
                    {
                        results.Add(item);
                    }

                    if (filterParts.OperatorValue == FilterOperator.LessThan && result < 0)
                    {
                        results.Add(item);
                    }
                }
            }
            this.ClearItems();
            foreach (T itemFound in results)
            {
                this.Add(itemFound);
            }
        }