我有一個解決問題IncludeProperty:
private Type persitentType = typeof(T);
public IList<T> GetByExample(T exampleInstance, params string[] propertiesToInclude)
{
// get the properties that will be excluded
List<string> propertiesToExclude =
persitentType.GetProperties().Where(p => propertiesToInclude.Contains(p.Name) == false).Select(p => p.Name).ToList();
// create the criteria based on the example and excluding the given properties
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
Example example = Example.Create(exampleInstance);
foreach (string propertyToExclude in propertiesToExclude)
{
example.ExcludeProperty(propertyToExclude);
}
criteria.Add(example);
// return the result
return criteria.List<T>();
}
添加這個方法到存儲庫類。它使用反射來確定指定對象具有的屬性,然後根據已指定爲包含的屬性找到要排除的屬性。
爲什麼不能排除SELECT中的列? – 2010-09-08 15:26:38
我不想在我的代碼中有任何SQL。通過包含SQL,我可以將自己與特定的數據庫技術捆綁在一起。 我只想使用這些對象。 – CarllDev 2010-09-08 16:22:00