/// <summary> /// Returns an enumerator that iterates through the collection. /// </summary> /// <returns> /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection. /// </returns> public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator() { // Consider: we could get the enumeration of key ranges, sort them and union overlapping ranges. // Enumerating the data as several different ranges would be more efficient when the expression // specifies an OR and the ranges are highly disjoint. KeyRange <TKey> range = KeyValueExpressionEvaluator <TKey, TValue> .GetKeyRange(this.expression); this.dictionary.TraceWhere(range, this.isReversed); if (this.isReversed) { return(new PersistentDictionaryReverseEnumerator <TKey, TValue, KeyValuePair <TKey, TValue> >( this.dictionary, range, c => c.RetrieveCurrent(), this.predicate)); } return(new PersistentDictionaryEnumerator <TKey, TValue, KeyValuePair <TKey, TValue> >( this.dictionary, range, c => c.RetrieveCurrent(), this.predicate)); }
/// <summary> /// Optimize a where statement which uses this dictionary. /// </summary> /// <param name="expression"> /// The predicate determining which items should be enumerated. /// </param> /// <returns> /// An enumerator matching only the records matched by the predicate. /// </returns> public PersistentDictionaryLinqKeyValueEnumerable <TKey, TValue> Where( Expression <Predicate <KeyValuePair <TKey, TValue> > > expression) { if (null == expression) { throw new ArgumentNullException("expression"); } Predicate <KeyValuePair <TKey, TValue> > predicate = KeyValueExpressionEvaluator <TKey, TValue> .KeyRangeIsExact(expression) ? x => true : expression.Compile(); return(new PersistentDictionaryLinqKeyValueEnumerable <TKey, TValue>( this, expression, predicate, false)); }