开发者

Linq to nhibernate - find values starting with number

开发者 https://www.devze.com 2023-02-26 01:41 出处:网络
I\'m using Linq to NHibernate and I have a need to find all records with a column (\"BusinessName\") that start with a number.

I'm using Linq to NHibernate and I have a need to find all records with a column ("BusinessName") that start with a number.

My first thought was something like this:

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

But I'm getting the exception:

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)

I don't really understand the exception, but I assume it has something to do with my use of substring to try match the first character against a list of numbers, since I can do the following without issue:

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

How would you do this? Optimally I'm looking for a LINQ query that will work, but I might be able to resort to using an NHibernate named query (stored proc) if that's the only way to do this.

Bump for the Monday crowd.


This should do the trick

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

0

精彩评论

暂无评论...
验证码 换一张
取 消