/// <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();
        }
        /// <summary>
        /// Verifica se contém um indice para a propriedade informada.
        /// </summary>
        /// <param name="propertyName">Nome da propriedade do indice.</param>
        /// <param name="indexType">Tipo de indice.</param>
        /// <returns></returns>
        public bool ContainsIndex(string propertyName, ObservableCollectionIndexType indexType)
        {
            Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > values = null;

            if (_indexes == null)
            {
                return(false);
            }
            lock (_indexes)
                if (!_indexes.TryGetValue(propertyName, out values) || values.Count == 0 || (indexType != ObservableCollectionIndexType.Any && !values.ContainsKey(indexType)))
                {
                    return(false);
                }
            return(true);
        }
        /// <summary>
        /// Remove o indice.
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="indexType"></param>
        public bool RemoveIndex(string propertyName, ObservableCollectionIndexType indexType)
        {
            Dictionary <ObservableCollectionIndexType, IObservableCollectionIndex <T> > values = null;

            if (_indexes != null)
            {
                lock (_indexes)
                    if (!_indexes.TryGetValue(propertyName, out values) && values.Remove(indexType))
                    {
                        if (values.Count == 0)
                        {
                            _indexes.Remove(propertyName);
                        }
                        return(true);
                    }
            }
            return(false);
        }
 /// <summary>
 /// Realiza uma pesquisa no
 /// </summary>
 /// <param name="propertyName"></param>
 /// <param name="indexType"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 System.Collections.IEnumerable IIndexedObservableCollection.Search(string propertyName, ObservableCollectionIndexType indexType, object key)
 {
     return(Search2(propertyName, indexType, key));
 }
        /// <summary>
        /// Realiza a pesquisa usando o indice com a chave informada.
        /// </summary>
        /// <param name="property">Propriedade indexada.</param>
        /// <param name="indexType"></param>
        /// <param name="key">Chave que será pesquisa.</param>
        /// <returns></returns>
        public IEnumerable <T> Search(System.Linq.Expressions.Expression <Func <T, object> > property, ObservableCollectionIndexType indexType, object key)
        {
            property.Require("property").NotNull();
            var propertyInfo = property.GetMember();

            return(Search2(propertyInfo.Name, indexType, key));
        }
        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);
                }
            }
        }
 /// <summary>
 /// Cria o indice para a propriedade informada.
 /// </summary>
 /// <typeparam name="PropertyType"></typeparam>
 /// <param name="type"></param>
 /// <param name="property"></param>
 public void CreateIndex <PropertyType>(System.Linq.Expressions.Expression <Func <T, PropertyType> > property, ObservableCollectionIndexType type)
 {
     CreateIndex <PropertyType>(property, type, Comparer <PropertyType> .Default);
 }