PeteGoo / NHibernate.QueryService

Investigation into an Nhibernate OData query only service
18 stars 4 forks source link

problem with .Any on IList not being recognized as a method #3

Open thechasboi opened 10 years ago

thechasboi commented 10 years ago

If I feed a list of objects of type NHibernate.Linq.NhQueryable and use .Any on this object inside the nhibernate query it works fine but when I use an IList the .Any is not recognized as a method. Code below I must preface with stating we are using Onion architecture and extracted nihibernate layer. There are interfaces that inherate from the repository wich inturn inherets from the extracted nhibernate methods. I will first include the .find which I am using to get the list by filtering from another list. We are also using dependency injection. ///

Finds this instance.

    /// <returns><see cref="IQueryable"/> of <see cref="T"/>.</returns>
    public virtual IQueryable<T> Find()
    {
        return Session.Query<T>();
    }

the first example works using the _contractorRepository is a repository object the contracts come from another repository query and returns nhibernate wrapped object/proxied list. _contractorRepository.Find().Where(x => contracts.Any(a => a.Contractor.Id == x.Id) && x.IsActive)

This result in a perfect set from the db.

The following is a query built for a search page. the entire query works except for this point. queryEveny = queryEveny.Where(x => userContracts.Any(a => a.Contractor.Id == x.Contract.Contractor.Id))

the userContracts is a list of contract objects like you would expect from an object but they are a plain IList. If I convert to an IQueryable I get different errors that eventually point to the fact the .Any is not a recognized function. I hope this makes sense.
Any thoughts on how to achieve this?

Thanks for the reply.

thechasboi commented 10 years ago

This is the error I get.

Specified method is not supported. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Specified method is not supported.

Source Error:

Line 466: }*/ Line 467: Line 468: searchResultsViewModel.SafetyEvents = queryEveny.Take(100).ToList(); Line 469: Line 470: return View("IndexEventResults", searchResultsViewModel);

Source File: c:\Projects\NMIS-IT\src\NASA.SMA.NMIS.Web.UI\Controllers\SearchController.cs Line: 468

Stack Trace:

[NotSupportedException: Specified method is not supported.] NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource) +256 NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree) +232 NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process() +142 NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process(IASTNode ast, ISessionFactoryImplementor factory) +129 NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary2 filters, ISessionFactoryImplementor factory) +227 NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary2 filters, ISessionFactoryImplementor factory) +160 NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary2 enabledFilters, ISessionFactoryImplementor factory) +210 NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary2 enabledFilters, ISessionFactoryImplementor factory) +120 NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary2 enabledFilters, ISessionFactoryImplementor factory) +97 NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary2 enabledFilters) +708 NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow) +294 NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression) +221 NHibernate.Linq.DefaultQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) +296 NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +83 NHibernate.Linq.DefaultQueryProvider.Execute(Expression expression) +75 Remotion.Linq.QueryableBase1.GetEnumerator() +212 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +446 System.Linq.Enumerable.ToList(IEnumerable1 source) +80 NASA.SMA.NMIS.Web.UI.Controllers.SearchController.Index(SearchViewModel model) in c:\Projects\NMIS-IT\src\NASA.SMA.NMIS.Web.UI\Controllers\SearchController.cs:468 lambda_method(Closure , ControllerBase , Object[] ) +127 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +242 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +39 System.Web.Mvc.Async.AsyncControllerActionInvoker.b36(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12 System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139 System.Web.Mvc.Async.AsyncInvocationWithFilters.b3c() +112 System.Web.Mvc.Async.<>cDisplayClass45.b3e() +452 System.Web.Mvc.Async.<>cDisplayClass45.b3e() +452 System.Web.Mvc.Async.<>cDisplayClass45.b3e() +452 System.Web.Mvc.Async.<>cDisplayClass30.b2f(IAsyncResult asyncResult) +15 System.Web.Mvc.Async.<>cDisplayClass28.b19() +37 System.Web.Mvc.Async.<>cDisplayClass1e.b1b(IAsyncResult asyncResult) +241 System.Web.Mvc.Controller.b1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +111 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +19 System.Web.Mvc.MvcHandler.b4(IAsyncResult asyncResult, ProcessRequestState innerState) +51 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288