简体   繁体   English

NHibernate ICriteria和预期类型

[英]NHibernate ICriteria and Expected Types

Is there any way to get at the types of objects I would expect NHibernate to place in an ICriteria object as the result of running a query? 是否有任何方法可以得出我希望NHibernate由于运行查询而将其放置在ICriteria对象中的对象类型? In this code sample, I can get at the types of my objects if they aren't null, but what if they are? 在此代码示例中,如果对象的类型不为null,我可以了解它们的类型,但是如果它们为null,该怎么办? Also, depending on the data returned, one "row" (object[]) may have null fields in places where other rows don't - forcing me to enumerate through all rows (worst case) to determine what each column (index of object[]) should be. 另外,根据返回的数据,一个“行”(object [])可能在其他行没有的地方具有空字段-迫使我枚举所有行(最坏的情况)以确定每一列(对象的索引) []) 应该。

Where can I find the expected type for the object (the expected type for each of the objects in the array I've created - obviously it wouldn't be on my array, but I would expect it to be somewhere in the ICriteria hierarchy)? 我在哪里可以找到对象的期望类型(我创建的数组中每个对象的期望类型-显然它不在我的数组中,但是我希望它在ICriteria层次结构中的某个位置) ?

DetachedCriteria dc = DetachedCriteria.For<MyObject>().Add(...).SetProjection(...);

IList<object[]> list = dc.GetExecutableCriteria(session).List().OfType<object[]>().ToList();

foreach(object [] o in list)
{
   foreach(object p in o)
   {
      if(p != null)
         Type t = p.GetType();
      else
         throw new ApplicationException("Query returned null for column");
   }
}

I ask this because having to actually examine the results returned by NHibernate seems like the wrong way to go about this. 我之所以这样问,是因为必须实际检查NHibernate返回的结果似乎是错误的方法。 Reflection doesn't appear to be any help either, because I can't find an instance to my object's types anywhere but on the actual result "row" / "column" when examining the ICriteria object (a CriteriaImpl object) returned. 反射似乎也没有任何帮助,因为在检查返回的ICriteria对象(一个CriteriaImpl对象)时,除了在实际结果“ row” /“ column”上,我无法在任何地方找到对象类型的实例。

I ask because I'm trying to dynamically create a DataTable from an NHibernate result, and I want to have the columns strongly typed. 我问是因为我试图根据NHibernate结果动态创建一个DataTable,而且我想对列进行强类型化。

The types returned are the types you queried for. 返回的类型是您要查询的类型。 If your users are adding custom columns and querying freely, you won't know what you're querying for, so you can't get a strongly-typed result without inspecting the actual objects. 如果您的用户要添加自定义列并自由查询,那么您将不知道要查询的内容,因此,如果不检查实际对象,就不会得到强类型的结果。

AFAIK this is a limitation in NHibernate. AFAIK这是NHibernate中的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM