public SortedBindingList <C> GetSortedSubList(FCriteria criteria, List <string> properties_list) { List <C> list = new List <C>(); SortedBindingList <C> sortedList = new SortedBindingList <C>(list); if (Items.Count == 0) { return(sortedList); } PropertyDescriptor property = null; if (criteria.GetProperty() != null) { property = TypeDescriptor.GetProperties(Items[0]).Find(criteria.GetProperty(), false); } else { property = null; } Type type = typeof(C); System.Reflection.PropertyInfo prop = null; switch (criteria.Operation) { case Operation.Equal: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (value.ToString().ToLower().Equals(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (value.ToString().ToLower().Equals(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); break; } } } } } break; case Operation.StartsWith: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (value.ToString().ToLower().StartsWith(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (value.ToString().ToLower().StartsWith(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); break; } } } } } break; case Operation.Less: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (criteria.Less(value)) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (criteria.Less(value)) { sortedList.Add(item); break; } } } } } break; case Operation.LessOrEqual: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (criteria.LessOrEqual(value)) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (criteria.LessOrEqual(value)) { sortedList.Add(item); break; } } } } } break; case Operation.Greater: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (criteria.Greater(value)) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (criteria.Greater(value)) { sortedList.Add(item); break; } } } } } break; case Operation.GreaterOrEqual: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (criteria.GreaterOrEqual(value)) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (criteria.GreaterOrEqual(value)) { sortedList.Add(item); break; } } } } } break; case Operation.Contains: default: { foreach (C item in Items) { foreach (string propName in properties_list) { prop = type.GetProperty(propName); if (prop == null) { continue; } //Buscamos en una propiedad en concreto if (property != null) { if (prop.Name == property.Name) { object value = prop.GetValue(item, null); if (value == null) { break; } if (value.ToString().ToLower().Contains(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); } break; } } //Buscamos en todas las propiedades de la lista else { object value = prop.GetValue(item, null); if (value == null) { continue; } if (value.ToString().ToLower().Contains(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); break; } } } } } break; } return(sortedList); }
public SortedBindingList <C> GetSortedSubList(FCriteria criteria) { List <C> list = new List <C>(); SortedBindingList <C> sortedList = new SortedBindingList <C>(list); if (Items.Count == 0) { return(sortedList); } PropertyDescriptor property = TypeDescriptor.GetProperties(Items[0]).Find(criteria.GetProperty(), false); switch (criteria.Operation) { case Operation.StartsWith: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (value.ToString().ToLower().StartsWith(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); } break; } } } } break; case Operation.Equal: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (criteria.Equal(value)) { sortedList.Add(item); } break; } } } } break; case Operation.Less: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (criteria.Less(value)) { sortedList.Add(item); } break; } } } } break; case Operation.LessOrEqual: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (criteria.LessOrEqual(value)) { sortedList.Add(item); } break; } } } } break; case Operation.Greater: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (criteria.Greater(value)) { sortedList.Add(item); } break; } } } } break; case Operation.GreaterOrEqual: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (criteria.GreaterOrEqual(value)) { sortedList.Add(item); } break; } } } } break; default: { foreach (C item in Items) { foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(item)) { if (prop.Name == property.Name) { object value = prop.GetValue(item); if (value.ToString().ToLower().Contains(criteria.GetValue().ToString().ToLower())) { sortedList.Add(item); } break; } } } } break; } return(sortedList); }