views:

1152

answers:

2
+3  Q: 

nhibernate Linq

I am using the old linq provider for nHibernate and waiting patiently for the new one to come out. I am sure it will save me much pain. Anyway, I have a problem with the code below as it generates a "Object reference not set to an instance of an object". The line "c.Disciplines.Any(d => disciplines.Contains(d))" is the one causing the problem. Is there any know issues with a statement like this using nHibernate Linq?

var agencies = m_AgencyRepository.Linq;

Discipline[] disciplines = user.Disciplines.ToArray<Discipline>();

return (from c in agencies
                   where (String.IsNullOrEmpty(criteria.AgencyName) || c.AgencyName.Contains(criteria.AgencyName))
                    && (criteria.AgencyType == null || c.AgencyType.AgencyTypeId == criteria.AgencyType)
                    && (String.IsNullOrEmpty(criteria.AgencyLevel) || c.AgencyLevel.AgencyLevelId == criteria.AgencyLevel)
                    && (c.Disciplines.Any(d => disciplines.Contains(d)))
select c)

Stack Trace:

   at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetEntityName(ICriteria subcriteria, String propertyName)
   at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetType(ICriteria subcriteria, String propertyName)
   at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetTypeUsingProjection(ICriteria subcriteria, String propertyName)
   at NHibernate.Criterion.InExpression.GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
   at NHibernate.Criterion.Junction.GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery)
   at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetQueryParameters()
   at NHibernate.Criterion.SubqueryExpression.InitializeInnerQueryAndParameters(ICriteriaQuery criteriaQuery)
   at NHibernate.Criterion.SubqueryExpression.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters)
   at NHibernate.Criterion.Junction.ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary`2 enabledFilters)
   at NHibernate.Loader.Criteria.CriteriaQueryTranslator.GetWhereCondition(IDictionary`2 enabledFilters)
   at NHibernate.Loader.Criteria.CriteriaJoinWalker..ctor(IOuterJoinLoadable persister, CriteriaQueryTranslator translator, ISessionFactoryImplementor factory, CriteriaImpl criteria, String rootEntityName, IDictionary`2 enabledFilters)
   at NHibernate.Loader.Criteria.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl rootCriteria, String rootEntityName, IDictionary`2 enabledFilters)
   at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
   at NHibernate.Impl.CriteriaImpl.List(IList results)
   at NHibernate.Impl.CriteriaImpl.List()
   at NHibernate.Impl.CriteriaImpl.UniqueResult()
   at NHibernate.Linq.Visitors.ImmediateResultsVisitor`1.HandleAggregateCall(MethodCallExpression call)
   at NHibernate.Linq.Visitors.ImmediateResultsVisitor`1.VisitMethodCall(MethodCallExpression call)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.ImmediateResultsVisitor`1.GetResults(MethodCallExpression expr)
   at NHibernate.Linq.Visitors.RootVisitor.HandleImmediateResultsCall(MethodCallExpression call)
   at NHibernate.Linq.Visitors.RootVisitor.VisitMethodCall(MethodCallExpression expr)
   at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp)
   at NHibernate.Linq.Visitors.NHibernateQueryTranslator.Translate(Expression expression, QueryOptions queryOptions)
   at NHibernate.Linq.NHibernateQueryProvider.Execute(Expression expression)
   at NHibernate.Linq.QueryProvider.System.Linq.IQueryProvider.Execute[T](Expression expression)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at PPSS.Services.AgencyRepository.List(AgencyListCriteria criteria, Int32& total) in C:\Development\PPSS\PPSS.Services\Services\Agency\AgencyRepository.cs:line 57
   at PPSS.Models.AgencyViewData..ctor(AgencyListCriteria criteria) in C:\Development\PPSS\PPSS\Domains\Maintenance\ViewModels\AgencyViewData.cs:line 26
   at PPSS.Controllers.AgencyController.Index(AgencyListCriteria criteria) in C:\Development\PPSS\PPSS\Domains\Maintenance\Controllers\AgencyController.cs:line 34
   at lambda_method(ExecutionScope , ControllerBase , Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
+1  A: 

Have you tried checking that c.Disciplines is non-null before doing an Any on it? Presumably disciplines is non-null, but you could also check it.

 (c.Disciplines != null
    && c.Disciplines.Any(d => disciplines != null
                               && disciplines.Contains(d)))
tvanfosson
Yes, I have tried that.
Craig
+2  A: 

Change

user.Disciplines.ToArray<Discipline>()

to

user.Disciplines.ToList<Discipline>()

NHibernate understands .Contains() calls on concrete List objects.

Bryan Watts