NHibernate ICriteria 和预期类型

发布于 2024-07-26 20:59:23 字数 916 浏览 6 评论 0原文

有什么方法可以获取我希望 NHibernate 在 ICriteria 对象中作为运行查询的结果放置的对象类型吗? 在此代码示例中,如果对象不为空,我可以获取它们的类型,但如果它们为空怎么办? 另外,根据返回的数据,一个“行”(object[])可能在其他行没有的地方具有空字段 - 迫使我枚举所有行(最坏的情况)以确定每一列(对象的索引) []) 应该。

我在哪里可以找到对象的预期类型(我创建的数组中每个对象的预期类型 - 显然它不会在我的数组上,但我希望它位于 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");
   }
}

我问这个问题是因为必须实际检查 NHibernate 返回的结果似乎是错误的方法。 反射似乎也没有任何帮助,因为在检查返回的 ICriteria 对象(CriteriaImpl 对象)时,除了在实际结果“行”/“列”上之外,我在任何地方都找不到对象类型的实例。

我问这个问题是因为我正在尝试从 NHibernate 结果动态创建 DataTable,并且我希望列强类型化。

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? In this code sample, I can get at the types of my objects if they aren't null, but what if they are? 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.

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)?

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. 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.

I ask because I'm trying to dynamically create a DataTable from an NHibernate result, and I want to have the columns strongly typed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2024-08-02 20:59:23

返回的类型是您查询的类型。 如果您的用户添加自定义列并自由查询,您将不知道自己在查询什么,因此如果不检查实际对象,就无法获得强类型结果。

AFAIK 这是 NHibernate 的一个限制。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文