Soyez le premier à donner votre avis sur cette source.
Snippet vu 11 139 fois - Téléchargée 23 fois
using System; using System.Collections.Generic; using System.Xml.Serialization; namespace System.Collections.Generic.Filtred { [Serializable] public class FiltrableList<T> { #region Fields private Type theType; private List<T> _List; private List<T> _FiltredList; private List<FilterValue> _Filters; #endregion #region Properties public List<T> List { get { return _List; } set { _List = value; } } public List<FilterValue> Filters { get { return _Filters; } set { _Filters = value; } } [XmlIgnore] public List<T> FiltredList { get { return _FiltredList; } } #endregion #region Constructor public FiltrableList() { _List = new List<T>(); _FiltredList = new List<T>(); _Filters = new List<FilterValue>(); theType = typeof(T); } #endregion #region Public Methods public void FilterBy(string Property, object Value) { _FiltredList = FilterBy(Property, Value, FilterComparaison.Equal, _List); } public void FilterBy(string Property, object Value, FilterComparaison Comp) { _FiltredList= FilterBy(Property, Value, Comp, _List); } public void ApplyFilters() { _FiltredList = _List; foreach (FilterValue fv in _Filters) _FiltredList = FilterBy(fv.Property, fv.Value, fv.FilterComparaison,_FiltredList); } public void SortListBy(string Property) { SortBy(Property, SortDirection.Asc, _List); } public void SortListBy(string Property, SortDirection direction) { SortBy(Property, direction, _List); } public void SortFiltredListBy(string Property) { SortBy(Property, SortDirection.Asc, _FiltredList); } public void SortFiltredListBy(string Property, SortDirection direction) { SortBy(Property, direction, _FiltredList); } #endregion #region Private Methods private void SortBy(string Property, SortDirection direction,List<T> list) { System.Reflection.PropertyInfo pi=theType.GetProperty(Property); if (pi == null) throw new Exception("Property " + Property + " does not exists in class " + typeof(T)); list.Sort(new Comparison<T>( delegate(T t1, T t2) { return ((IComparable)pi.GetValue(t1, null)).CompareTo(pi.GetValue(t2, null)) * (int)direction; } )); } private List<T> FilterBy(string Property, object Value, FilterComparaison Comp, List<T> list) { System.Reflection.PropertyInfo pi = theType.UnderlyingSystemType.GetProperty(Property); if (pi == null) throw new Exception("Property " + Property + " does not exists in class " + typeof(T)); return list.FindAll(new Predicate<T>(delegate(T o) { object theValue = pi.GetValue(o, null); int res = ((IComparable)theValue).CompareTo(Convert.ChangeType(Value, theValue.GetType())); switch (Comp) { case FilterComparaison.Inf: return res < 0; case FilterComparaison.InfEqual: return res <= 0; case FilterComparaison.Sup: return res > 0; case FilterComparaison.SupEqual: return res >= 0; case FilterComparaison.Like: return ((string)theValue).ToLower().Contains(((string)Value).ToLower()); case FilterComparaison.NotLike: return !((string)theValue).ToLower().Contains(((string)Value).ToLower()); default: return res == 0; } } )); } #endregion } [Serializable] public class FilterValue { #region Fields private string _Property; private object _Value; private FilterComparaison _FilterComparaison; #endregion #region Properties public string Property { get { return _Property; } set { _Property = value; } } public object Value { get { return _Value; } set { _Value = value; } } public FilterComparaison FilterComparaison { get { return _FilterComparaison; } set { _FilterComparaison = value; } } #endregion #region Constructors public FilterValue(string Property, object Value, FilterComparaison Comp) { _Property = Property; _Value = Value; _FilterComparaison = Comp; } public FilterValue(string Property, object Value) { _Property = Property; _Value = Value; _FilterComparaison = FilterComparaison.Equal; } #endregion } public enum FilterComparaison { Inf, Sup, Equal, Like, InfEqual, SupEqual, NotLike } public enum SortDirection { Asc = 1, Desc = -1 } }
merci
Ah... au fait, évite le mélange francais / anglais :D... c'est Liste Générique Filtrable... ou Filterable Generic List
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.