public List <T> GetByInclusionExample(T exampleInstance, string propertyName, bool ascending, params string[] propertiesToInclude) { //Get all of the properties of the given type (T) System.Reflection.PropertyInfo[] properties = persitentType.GetProperties(); //System.Reflection.PropertyInfo[] properties = exampleInstance.GetType().GetProperties(); ICriteria criteria = NHibernateSession.CreateCriteria(persitentType); Example example = Example.Create(exampleInstance); List <string> includedProperties = new List <string>(propertiesToInclude); //Exclude app properties except for the included properties foreach (System.Reflection.PropertyInfo property in properties) { if (!includedProperties.Contains(property.Name)) { //only exclude the property if it isn't in the propertiesToInclude list example.ExcludeProperty(property.Name); } } criteria.Add(example); if (!string.IsNullOrEmpty(propertyName)) { criteria.AddOrder(new Order(propertyName, ascending)); } ListToGenericListConverter <T> converter = new ListToGenericListConverter <T>(); return(converter.ConvertToGenericList(criteria.List())); }
public List <T> GetByExample(T exampleInstance, params string[] propertiesToExclude) { ICriteria criteria = NHibernateSession.CreateCriteria(persitentType); Example example = Example.Create(exampleInstance); foreach (string propertyToExclude in propertiesToExclude) { example.ExcludeProperty(propertyToExclude); } criteria.Add(example); ListToGenericListConverter <T> converter = new ListToGenericListConverter <T>(); return(converter.ConvertToGenericList(criteria.List())); }
/// <summary> /// Overload that allows for sorting by one property /// If no <see cref="ICriterion" /> is supplied, this behaves like <see cref="GetAll" />. /// </summary> public List <T> GetByCriteria(string propertyName, bool ascending, params ICriterion[] criterion) { ICriteria criteria = NHibernateSession.CreateCriteria(persitentType); foreach (ICriterion criterium in criterion) { criteria.Add(criterium); } if (!string.IsNullOrEmpty(propertyName)) { //If the propertyName is not empty, sort by that property criteria.AddOrder(new Order(propertyName, ascending)); } ListToGenericListConverter <T> converter = new ListToGenericListConverter <T>(); return(converter.ConvertToGenericList(criteria.List())); }