In Linq-to-Nhibernate, has anyone managed to use .Contains in a where clause and also NHibernate.Linq.ToFuture() in the same query? (NH 3.x)

StackOverflow https://stackoverflow.com/questions/8720874

  •  13-04-2021
  •  | 
  •  

Question

I always get a "System.Collections.Generic.KeyNotFoundException" that says "The given key was not present in the dictionary" whenever I try to use .Contains and the ToFuture() method in one query.

Imagine the DBObject contains a bunch of properties with one of them being an integer "ID"

List<int> test = new List<int>();
        test.Add(1557);
        test.Add(1558);
        test.Add(1559);

        IEnumerable<DBObject> test2 = getLinqQuerySomehow<DBObject>().Where(x => test.Contains(x.ID)).ToFuture();
        List<DBObject> results = test2.ToList();

Can anyone reproduce this? Does anyone know of another way besides contains() to make Linq-to-Nhibernate use the SQL IN clause for the ints in my test list while also using ToFuture()?

Stacktrace:

at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at NHibernate.Param.NamedParameterSpecification.SetEffectiveType(QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Param\NamedParameterSpecification.cs:line 70 at NHibernate.Param.ParametersBackTrackExtensions.ResetEffectiveExpectedType(IEnumerable`1 parameterSpecs, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Param\ParametersBackTrackExtensions.cs:line 48 at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.ResetEffectiveExpectedType(IEnumerable`1 parameterSpecs, QueryParameters queryParameters) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\Loader\QueryLoader.cs:line 428 at NHibernate.Loader.Loader.CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1649 at NHibernate.Impl.MultiQueryImpl.AggregateQueriesInformation() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\MultiQueryImpl.cs:line 641 at NHibernate.Impl.MultiQueryImpl.get_Parameters() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\MultiQueryImpl.cs:line 774 at NHibernate.Impl.MultiQueryImpl.CreateCombinedQueryParameters() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\MultiQueryImpl.cs:line 754 at NHibernate.Impl.MultiQueryImpl.List() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\MultiQueryImpl.cs:line 400 at NHibernate.Impl.FutureQueryBatch.GetResultsFrom(IMultiQuery multiApproach) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\FutureQueryBatch.cs:line 24 at NHibernate.Impl.FutureBatch`2.GetResults() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\FutureBatch.cs:line 73 at NHibernate.Impl.FutureBatch`2.get_Results() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\FutureBatch.cs:line 29 at NHibernate.Impl.FutureBatch`2.GetCurrentResult[TResult](Int32 currentIndex) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\FutureBatch.cs:line 79 at NHibernate.Impl.FutureBatch`2.c__DisplayClass4`1.b__3() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\FutureBatch.cs:line 63 at NHibernate.Impl.DelayedEnumerator`1.d__0.MoveNext() in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\DelayedEnumerator.cs:line 26 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at TestProject1.UnitTest1.TestMethod1() in C:\checkout\Library Projects\BaseSystemCore\TestProject1\UnitTest1.cs:line 94
Was it helpful?

Solution

This is a bug. I found it in the Nhibernate issue tracker:

https://nhibernate.jira.com/browse/NH-2897

Edit: Should be fixed in NH 4.0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top