【发布时间】:2013-05-15 20:42:41
【问题描述】:
我在应用程序中使用 nhibernate,并且我有一个具有一些关系的映射模型。这些关系与List<T> 映射,我需要将一个实体传递给一个方法并调用Any() 方法来检查每个关系上是否有寄存器。
我尝试这样做,但是当我从 PropertyInfo 调用 GetValue() 方法时,NHibernate 将加载所有内容,但我只需要调用 Any() 方法来提高性能,并且 nhibernate 将查询一个简单的查询来检查。我试试这个:
var type = entity.GetType();
foreach (var propertyInfo in type.GetProperties().Where(p => typeof (IEnumerable<>).IsAssignableFrom(p.PropertyType)))
{
// it works, but load everything just to check if there are something...
var collection = propertyInfo.GetValue(entity) as IEnumerable<dynamic>;
if (collection != null)
bool has = collection.Any();
}
我想在这里调用 IEnumerable.Any(),但是如果没有 GetValue,我该如何使用反射来做到这一点?!
【问题讨论】:
-
修正你的标题;它与您的问题不符。
-
您是否将您的 NHibernate 属性/集合定义为延迟加载?
-
@Chris Sinclair,是的,这是延迟加载!
标签: c# linq nhibernate reflection collections