Skip to content

NH-3088 - Support the item operator [] on lists in linq queries #819

@nhibernate-bot

Description

@nhibernate-bot

Ehsan Zeynali created an issue — 14th March 2012, 6:27:12:

I have a Person entity. Person has a aggregation by many Records.

public class Person() { virtual public IList<Record> Records {get; set;} } 

and

public class Record() { virtual public int Number {get; set;} }

I have a linq query by NHibernate.

var q = SessionInstance.Query<Person>()         
           .Where(x => x.Records.Any() && x.Records[0].Number>= 5); 
q.ToList<Person>() 

It is a runtime error by this message exception : Domain.Entities.Record get_Item(Int32)

Why?


Oskar Berggren added a comment — 14th March 2012, 9:19:24:

Surely the exceptions contains more information than that? Full message, stack trace, and any inner exception?

But, there has been many linq fixes in 3.2 and 3.3CR1, so perhaps you could test with those?


Ehsan Zeynali added a comment — 14th March 2012, 11:47:40:

Full message is : Domain.Entities.Record get_Item(Int32)

Inner exception is : null

Stack Trace is :

   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitMemberExpression(MemberExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression)
   at NHibernate.Linq.Visitors.HqlGeneratorExpressionTreeVisitor.VisitExpression(Expression expression)
   at NHibernate.Linq.Visitors.QueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index)
   at Remotion.Data.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
   at Remotion.Data.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
   at Remotion.Data.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
   at NHibernate.Linq.Visitors.QueryModelVisitor.GenerateHqlQuery(QueryModel queryModel, VisitorParameters parameters, Boolean root)
   at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory)
   at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
   at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
   at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
   at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
   at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery)
   at NHibernate.Linq.NhQueryProvider.Execute<TResult>(Expression expression)
   at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList<TSource>(IEnumerable`1 source)
   at NHibernate.Repository.PersonRepositoryNh.Search(FullSearchPersonDto dto) in D:\Users\zeynali\src\NHibernate\Repository\Person\PersonRepositoryNh.cs:line 585
   at Domain.Services.PersonService.FullSearchCommand(IRepositoryLocator locator, FullSearchPersonDto dto) in D:\Users\zeynali\src\RCISP.Domain\Services\Person\PersonService.cs:line 107
   at Domain.Services.PersonService.<>c*_DisplayClass19.<FullSearch>b_*18(IRepositoryLocator locator) in D:\Users\zeynali\src\RCISP.Domain\Services\Person\PersonService.cs:line 103
   at NHibernate.TransManager.TransManagerNh.ExecuteCommand<TResult>(Func`2 command) in D:\Users\zeynali\src\RCISP.NHibernate\TransManager\TransManagerNh.cs:line 34

Alexander Zaytsev added a comment — 14th March 2012, 16:09:30:

Oskar Berggren, I think that it is not implemented :(


Oskar Berggren added a comment — 14th March 2012, 16:24:16:

Alexander seems to be correct. Sounds like it shouldn't be too difficult to implement, but it will not be in 3.3.0.


Alexander Zaytsev added a comment — 14th March 2012, 19:22:11:

Also it should handle ElementAt() in the same way. But indexer [] and ElementAt() should be available only for indexded collections (list map and array). For other collections there should be an order by statement.


Alexander Zaytsev added a comment — 14th March 2012, 19:27:28:

Related to NH-2187 (#897)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions