/// <summary> /// Realiza a pesquisa no indice associado com o nome da propriedade. /// </summary> /// <param name="propertyName"></param> /// <param name="indexType"></param> /// <param name="key"></param> /// <returns></returns> private IEnumerable <T> Search2(string propertyName, ObservableCollectionIndexType indexType, object key) { Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > values = null; if (_indexes == null) { throw new IndexNotFoundException(propertyName); } lock (_indexes) if (!_indexes.TryGetValue(propertyName, out values) || values.Count == 0 || (indexType != ObservableCollectionIndexType.Any && !values.ContainsKey(indexType))) { throw new IndexNotFoundException(propertyName); } IObservableCollectionIndex <T> index = null; if (indexType == ObservableCollectionIndexType.Any) { index = values.Values.FirstOrDefault(); } else { index = values[indexType]; } return(index[key]); }
/// <summary> /// Reseta o indice. /// </summary> /// <param name="propertyName">Nome da propriedade do indice.</param> /// <param name="indexType">Tipo do indice.</param> public void ResetIndex(string propertyName, ObservableCollectionIndexType indexType) { Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > values = null; if (_indexes == null) { throw new IndexNotFoundException(propertyName); } lock (_indexes) if (!_indexes.TryGetValue(propertyName, out values) || values.Count == 0 || (indexType != ObservableCollectionIndexType.Any && !values.ContainsKey(indexType))) { throw new IndexNotFoundException(propertyName); } IObservableCollectionIndex <T> index = null; if (indexType == ObservableCollectionIndexType.Any) { index = values.Values.FirstOrDefault(); } else { index = values[indexType]; } index.Reset(); }
public void CreateIndex <PropertyType>(System.Linq.Expressions.Expression <Func <T, PropertyType> > property, ObservableCollectionIndexType type, IComparer <PropertyType> comparer) { property.Require("property").NotNull(); var propertyInfo = property.GetMember() as System.Reflection.PropertyInfo; if (propertyInfo == null) { throw new InvalidOperationException("Invalid property"); } var indexName = propertyInfo.Name; if (_indexes != null) { lock (_indexes) { Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > values = null; if (_indexes.TryGetValue(indexName, out values) && values.ContainsKey(type)) { return; } } } IObservableCollectionIndex <T> index = null; var propertyGetter = property.Compile(); var getter = new Func <T, object>(f => propertyGetter(f)); if (type == ObservableCollectionIndexType.Sorted || type == ObservableCollectionIndexType.Hash) { index = new ObservableCollectionSortedIndex <T>(indexName, this, new string[] { property.Name }, getter, Comparer <PropertyType> .Default); } if (_indexes == null) { _indexes = new Dictionary <string, Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > >(); } lock (_indexes) { Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > values = null; if (!_indexes.TryGetValue(indexName, out values)) { values = new Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> >(); _indexes.Add(indexName, values); } if (!values.ContainsKey(type)) { values.Add(type, index); } } }