2009-05-29 38 views
5

我正在使用nHibernate的舊linq提供程序,耐心等待 爲新的出來。我相信它會爲我節省很多痛苦。 無論如何,我在下面的代碼中遇到問題,因爲它會生成一個「對象 引用未設置爲對象的實例」。該行 「c.Disciplines.Any(d => disciplines.Contains(d))」是造成 問題的原因之一。是否有任何知道這樣的聲明使用 nHibernate Linq?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) 

堆棧跟蹤:

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) 
+0

你是否在說「c.Disciplines.Any(d => disciplines.Contains(d))」是你的問題,因爲刪除它不會導致錯誤,或者因爲這是行號報告? – 2009-05-29 03:10:10

+0

任何解決方案呢? – Bertvan 2010-07-15 10:06:04

回答

1

您是否嘗試過檢查c.Disciplines非空做之前任何的呢?推測disciplines是非空的,但你也可以檢查它。

(c.Disciplines != null 
    && c.Disciplines.Any(d => disciplines != null 
           && disciplines.Contains(d))) 
5

變化

user.Disciplines.ToArray<Discipline>() 

user.Disciplines.ToList<Discipline>() 

NHibernate的理解混凝土List對象.Contains()電話。