nhibernatecriterianhibernate-criteria

Nhibernate Restrictions.In Error


Finally tracked down my error which is a result of the query. I have an nhibernate query using a Restrictions.In. Problem is once query executes if no results returned query throws error immediately. Is there another restriction that I can use. I know if I was writing a linq query I could use the .Any to return bool value and go from there is there something similar I can do in this instance?

carMake is passed in

        myQuery.JoinQueryOver(x => x.Car)
           .Where(Restrictions.In("VIN",
               Trades.Where(x => x.Car.Make.ToLower() == carMake.ToLower())
                   .Select(x => x.Car.PrimaryVIN)
                   .ToList()));

Solution

  • Assuming that Trades is a list of objects you can use .WhereRestrictionOn() instead. Try this (I split the code for better readability):

    var vinsWithCarMake = Trades
        .Where(x => x.Car.Make.ToLower() == carMake.ToLower())
        .Select(x => x.Car.PrimaryVIN)
        .ToList<string>();
    
    var carAlias = null;
    var result = myQuery.JoinAlias(x => x.Car, () => carAlias)
           .WhereRestrictionOn(() => carAlias.VIN).IsInG<string>(vinsWithCarMake)
           .List();