示例#1
0
 /// <summary>
 /// Selects the items that match the specified filter.
 /// </summary>
 public IEnumerable <T> Select(TableFilter filter, bool indexRequired = false)
 {
     foreach (object item in SelectItems(filter, indexRequired))
     {
         yield return((T)item);
     }
 }
示例#2
0
        /// <summary>
        /// Selects the first item that match the specified filter, or null.
        /// </summary>
        public T SelectFirst(TableFilter filter, bool indexRequired = false)
        {
            foreach (object item in SelectItems(filter, indexRequired))
            {
                return((T)item);
            }

            return(default);
示例#3
0
        /// <summary>
        /// Selects the items that match the specified filter.
        /// </summary>
        public IEnumerable SelectItems(TableFilter filter, bool indexRequired = false)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            // find the property used by the filter
            PropertyDescriptor filterProp = TypeDescriptor.GetProperties(ItemType)[filter.ColumnName];

            if (filterProp == null)
            {
                throw new ArgumentException("The filter property not found.");
            }

            // get the matched items
            if (TryGetIndex(filter.ColumnName, out TableIndex index))
            {
                int indexKey = filter.Argument == null ? 0 : (int)filter.Argument;

                foreach (object item in index.SelectItems(indexKey))
                {
                    yield return(item);
                }
            }
            else if (indexRequired)
            {
                throw new ScadaException("Index not found.");
            }
            else
            {
                object filterArg = filter.Argument;

                foreach (T item in Items.Values)
                {
                    object propVal = filterProp.GetValue(item);
                    if (Equals(propVal, filterArg))
                    {
                        yield return(item);
                    }
                }
            }
        }