当前不支持 LastResultOperator 结果运算符

发布于 2024-12-27 20:47:13 字数 747 浏览 3 评论 0 原文

我有一个使用 linq to NHibernate 的查询,用于 EnterAndExitArchive 实体。该实体与 Archive 实体有关联。

public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
   var q = SessionInstance.Query<EnterAndExitArchive>()
          .Where(x => x.Archive.Id == archiveId)
          .LastOrDefault<EnterAndExitArchive>();

   return q;
}

或者

public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
   var q = SessionInstance.Query<EnterAndExitArchive>()
          .LastOrDefault<EnterAndExitArchive>(x => x.Archive.Id == archiveId);

   return q;
}

但这有一个运行时错误。异常消息是当前不支持LastResultOperator 结果运算符

为什么?

I have a query using linq to NHibernate, for EnterAndExitArchive entity. This entity has a association by Archive entity.

public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
   var q = SessionInstance.Query<EnterAndExitArchive>()
          .Where(x => x.Archive.Id == archiveId)
          .LastOrDefault<EnterAndExitArchive>();

   return q;
}

Or

public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
   var q = SessionInstance.Query<EnterAndExitArchive>()
          .LastOrDefault<EnterAndExitArchive>(x => x.Archive.Id == archiveId);

   return q;
}

But this has a runtime error. Message of exception is The LastResultOperator result operator is not current supported.

Why?

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

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

发布评论

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

评论(2

瑾夏年华 2025-01-03 20:47:13

LastOrDefault()

也许您可以对结果进行排序并使用 FirstOrDefault() 代替:

public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
   var q = SessionInstance.Query<EnterAndExitArchive>()
          .Where(x => x.Archive.Id == archiveId)
          .OrderByDescending(x => x.Something)
          .FirstOrDefault();

   return q;
}

LastOrDefault() is not supported in NHibernate.

Maybe you could order the result and use FirstOrDefault() instead:

public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
   var q = SessionInstance.Query<EnterAndExitArchive>()
          .Where(x => x.Archive.Id == archiveId)
          .OrderByDescending(x => x.Something)
          .FirstOrDefault();

   return q;
}
笑,眼淚并存 2025-01-03 20:47:13

看来 nhibernate Linq 提供程序没有实现 LastOrDefault() - 因此它不受支持。您可以通过首先建立一个订单来解决此问题,该订单将以相反的顺序返回您想要的项目,然后使用 FirstOrDefault() 代替:

var q = SessionInstance.Query<EnterAndExitArchive>()
          .OrderByDescending(x=> x.SomeOrderField)
          .FirstOrDefault<EnterAndExitArchive>(x => x.Archive.Id == archiveId);

另外,我发现您当前没有在查询中对结果进行排序到底 - 你期望结果是什么顺序?如果顺序未定义,LastOrDefault()FirstOrDefault() 相同;-)

It seems the nhibernate Linq provider did not implement LastOrDefault() - as a result it is not supported. You can work around this by first of all establishing an order that will return the items you want in reverse order and then using FirstOrDefault() instead:

var q = SessionInstance.Query<EnterAndExitArchive>()
          .OrderByDescending(x=> x.SomeOrderField)
          .FirstOrDefault<EnterAndExitArchive>(x => x.Archive.Id == archiveId);

Also I see you are currently not ordering your results in your query at all - what order did you expect the results to be in? If the order is undefined LastOrDefault() is the same as FirstOrDefault() ;-)

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