The result statement LastResultOperator is not supported by the current

I have a query using linq for NHibernate for an EnterAndExitArchive object. This object has an association for an Archive object.

 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. Exception Message - The LastResultOperator result operator is not current supported .

Why?

+4
source share
2 answers

LastOrDefault() not supported in NHibernate.

Perhaps 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; } 
+11
source

It seems that the nhibernate Linq provider did not implement LastOrDefault() - as a result, it is not supported. You can get around this by first setting an order that will return the elements you need in the reverse order, and then use FirstOrDefault() instead:

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

I also see that you are not currently ordering your results in your query at all - in what order did you expect the results to be? If the order of undefined LastOrDefault() matches FirstOrDefault() ; -)

+2
source

Source: https://habr.com/ru/post/1391841/


All Articles