Linq to nhibernate - 查找以数字开头的值

时间:2011-04-17 23:09:13

标签: linq-to-nhibernate

我正在使用Linq到NHibernate,我需要找到一个以数字开头的列(“BusinessName”)的所有记录。

我的第一个想法是这样的:

var numbers = Enumerable.Range(0, 10).Select(x => x.ToString()).ToList();
query = query.Where(x=>numbers.Contains(x.BusinessName.Substring(0,1)));

但我得到了例外:

System.NullReferenceException: Object reference not set to an instance of an object.
   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.AssertPropertyIsNotCollection(ICriteriaQuery criteriaQuery, ICriteria criteria)
   at NHibernate.Criterion.InExpression.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, ICriteria 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.TranslateInternal(Expression expression)
   at NHibernate.Linq.Visitors.NHibernateQueryTranslator.Translate(Expression expression, QueryOptions queryOptions)
   at NHibernate.Linq.NHibernateQueryProvider.TranslateExpression(Expression expression)
   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 SBON.ApiWrapper.SearchBusinesses(Nullable`1 id, String filter, String type, String letter, Nullable`1 start, Nullable`1 count) in D:\Data\Projects\Active\Clients\OfficeDepotSBON\src\SBON\ApiWrapper.cs:line 313
   at SBONWeb.Controllers.MobileController.SearchBusinesses(String letter, String filter, Nullable`1 start, Nullable`1 count) in D:\Data\Projects\Active\Clients\OfficeDepotSBON\src\SBONWeb\Controllers\MobileController.cs:line 38
   at lambda_method(Closure , 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__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

我真的不明白异常,但我认为这与我使用substring尝试将第一个字符与数字列表匹配有关,因为我可以做到以下没有问题:

var numbers = Enumerable.Range(0, 10).Select(x => x.ToString()).ToList();
query = query.Where(x=>numbers.Contains(x.BusinessName));

你会怎么做?最理想的是我正在寻找可以工作的LINQ查询,但是如果这是唯一的方法,我可以使用NHibernate命名查询(存储过程)。

为周一人群拍打。

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题

query = query.Where(x => char.IsNumber(x.BusinessName[0]));