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

发布于 2024-11-02 03:11:53 字数 4568 浏览 0 评论 0原文

我正在使用 Linq to 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)

我不太理解这个异常,但我认为它与我使用子字符串尝试将第一个字符与数字列表匹配有关,因为我可以毫无问题地执行以下操作:

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

你会怎么做?最佳情况下,我正在寻找一个可以工作的 LINQ 查询,但如果这是唯一的方法,我可能可以求助于使用 NHibernate 命名查询(存储过程)。

周一人群的碰撞。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2024-11-09 03:11:53

这应该可以解决问题

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

This should do the trick

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文