11

I've got quite weird exception when trying to materialize the IQueryable I got form NHibernate.Linq. The exception of type Antlr.Runtime.Tree.RewriteEmptyStreamException just states plan b, and nothing more. Detailed exception can be found at http://pastebin.com/kR2dvDHd

Here's the code that throws an exception:

var matterExtractor = new MatterExtractor();
var InactiveMatters = matterExtractor.GetMattersAtStatus(General.InactiveMatterStatus);
Assert.IsNotNull(InactiveMatters); //OK
Assert.IsInstanceOfType(InactiveMatters, typeof (IQueryable<Matter>)); // OK
var MaterializedMatters = InactiveMatters.ToList(); //Exception is thrown

Matter Extractor class is as simple as follwing:

public class MatterExtractor
{
    public virtual IQueryable<Matter> GetMattersAtStatus(MatterStatus status)
    {
        return
            (new NHibernateRepository.Repository<Matter>()).Where(
                m => m.MatterStatusHistories.OrderByDescending(msh => msh.CreateTime).FirstOrDefault().MatterStatus == status);
    }
}

NHibernateRepository.Repository<T> is an utility class that implements IQueryable via NHibernate.LINQ extension methods to NHibernate.Session. Nothing specific here, but just in case, here's the listing: http://pastebin.com/MgDxDg3Y

I don't think it's related to NHibernate mappings, since other tests that interact with Matter entity run just fine. Most probably it's related to the Where clause, but I can't understand what's going wrong with that clause. I've tried replacing

OrderByDescending(msh => msh.CreateTime).FirstOrDefault()

to

OrderBy(msh => msh.CreateTime).LastOrDefault()

but it just told me The LastResultOperator result operator is not current supported, so I think NHibernate.Linq just can't stay LastOrDefault.

Any ideas what does plan b mean and how can I workaround it?

David_001
  • 5,703
  • 4
  • 29
  • 55
J0HN
  • 26,063
  • 5
  • 54
  • 85
  • 3
    I could repro your issue so I would say this is a bug in NHibernate, either it should generate the query or it should throw a not supported exception. You should submit a bug report. As a workaround you can evaluate your whole query on client side or reorganize your query to start with `Repository`... – nemesv Jul 17 '12 at 07:36

1 Answers1

1

Are you certain that OrderByDescending(msh => msh.CreateTime).FirstOrDefault()

Is not returning null for any elements in your repository? That bit of code seems to me to be the bit in question.

(...OrderByDescending(msh => msh.CreateTime).FirstOrDefault() ?? someDummyStatusNotSatisfyingClause)

Might solve your problem.

Another possibility is that you haven't instructed NHibernate how/when to materialiaze the status histories in the entity definition. My experience with NHibernate is that some query like you are attempting might be better suited as a repository function (a stored procedure)

Scottley
  • 91
  • 5
  • The question was asked two years ago. Yes, I'm sure that `OrderByDescending(...)` never returned null. Unfortunately, I don't have access to the that code any more. Also, creating a stored procedure was not an option, as the app I was developing back than was a standalone addition to a much larger system; it was using main system database directly, so I was not allowed to make any schema changes. – J0HN Aug 24 '14 at 12:06