Error: An item with the same key has already been added

417 views
Skip to first unread message

-glenn-

unread,
Mar 9, 2012, 8:36:52 AM3/9/12
to nhu...@googlegroups.com
Below is an exception I get when calling Count on an NhQueryable object. It appears to be this line in the ExpressionParameterVisitor.cs class:

_parameters.Add(expression, new NamedParameter("p" + (_parameters.Count + 1), expression.Value, type));

This happens while trying to get the data to populate a Telerik MVC Grid. The grid allows columns to be filtered by one or two strings. If no filter is specified on the grid or if a filter is specified with only one string then the code runs fine. It's when two strings are specified in a filter (say, a "starts with" string and an "ends with" string), the NH error occurs. The where part of the query is created by a call to Telerik code:

Expression<Func<T, bool>> where = ExpressionBuilder.Expression<T>(command.FilterDescriptors);

The command parameter is passed into the AJAX method with the grid filter stings, and Telerik's ExpressionBuilder parses it into an Expression.

Then essentially the app does this:

IQueryable<T> query = session.Query<T>();
if (where != null) query = query.Where(where);
int rowCount = query.Count();

and it's that Count that gives the error. With my limited understanding of what is going on with the expression code, it looks correct to me. The left and right parts of the parsed Boolean expression are:

((IIF((item != null), item.AccountName, null) ?? "").ToLower().StartsWith("abc".ToLower()) == True)
((IIF((item != null), item.AccountName, null) ?? "").ToLower().EndsWith("def".ToLower()) == True)

But somehow, when NH's VisitConstantExpression is called, the _parameters Dictionary already has the ConstantExpression in it, hence the error message.

Thanks.

-glenn-



System.ArgumentException was unhandled by user code
  Message=An item with the same key has already been added.
  Source=mscorlib
  StackTrace:
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
       at NHibernate.Linq.Visitors.ExpressionParameterVisitor.VisitConstantExpression(ConstantExpression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\ExpressionParameterVisitor.cs:line 42
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitBinaryExpression(BinaryExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitLambdaExpression(LambdaExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitUnaryExpression(UnaryExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](T expression, String methodName) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.<>c__DisplayClass6`1.<VisitAndConvert>b__5(T expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitList[T](ReadOnlyCollection`1 list, Func`2 visitMethod) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](ReadOnlyCollection`1 expressions, String callerName) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](T expression, String methodName) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.<>c__DisplayClass6`1.<VisitAndConvert>b__5(T expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitList[T](ReadOnlyCollection`1 list, Func`2 visitMethod) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](ReadOnlyCollection`1 expressions, String callerName) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](T expression, String methodName) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.<>c__DisplayClass6`1.<VisitAndConvert>b__5(T expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitList[T](ReadOnlyCollection`1 list, Func`2 visitMethod) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitAndConvert[T](ReadOnlyCollection`1 expressions, String callerName) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitMethodCallExpression(MethodCallExpression expression) in :line 0
       at Remotion.Data.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) in :line 0
       at NHibernate.Linq.Visitors.NhExpressionTreeVisitor.VisitExpression(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\NhExpressionTreeVisitor.cs:line 32
       at NHibernate.Linq.Visitors.ExpressionParameterVisitor.Visit(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\Visitors\ExpressionParameterVisitor.cs:line 20
       at NHibernate.Linq.NhLinqExpression..ctor(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhLinqExpression.cs:line 37
       at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, IQuery& query, NhLinqExpression& nhQuery) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:line 41
       at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:line 26
       at NHibernate.Linq.NhQueryProvider.Execute[TResult](Expression expression) in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs:line 103
       at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
       at Website.Services.LookupTableServiceBase`1.GetLookupGrid(GridCommand command, IUserInfo`1 userInfo, String userNameMember) in G:\Website\Services\LookupTableServiceBase.cs:line 169
       at Website.Controllers.LookupTablesController.LookupIndex[T](GridCommand command, ILookupTableService`1 lookupService) in G:\Website\Controllers\LookupTablesController.cs:line 81
       at Website.Controllers.LookupTablesController._NicknameIndex(GridCommand command) in G:\Website\Controllers\LookupTablesController.cs:line 475
       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)
  InnerException:

Oskar Berggren

unread,
Mar 9, 2012, 4:43:55 PM3/9/12
to nhu...@googlegroups.com
2012/3/9 -glenn- <gdo...@gmail.com>:

> Below is an exception I get when calling Count on an NhQueryable object. It
> appears to be this line in the ExpressionParameterVisitor.cs class:
>
> _parameters.Add(expression, new NamedParameter("p" + (_parameters.Count +
> 1), expression.Value, type));
>


If you can, it would be helpful if you could construct a minimal test
case for inclusion in NHibernate, and open a Jira issue.

/Oskar

Glenn Doten

unread,
Mar 9, 2012, 3:21:55 PM3/9/12
to nhusers

Below is an exception I get when calling Count on an NhQueryable object. It appears to be this line in the ExpressionParameterVisitor.cs class:

_parameters.Add(expression, new NamedParameter("p" + (_parameters.Count + 1), expression.Value, type));

This happens while trying to get the data to populate a Telerik MVC Grid. The grid allows columns to be filtered by one or two strings. If no filter is specified on the grid or if a filter is specified with only one string then the code runs fine. It's when two strings are specified in a filter (say, a "starts with" string and an "ends with" string), the NH error occurs. The where part of the query is created by a call to Telerik code:

Expression<Func<T, bool>> where = ExpressionBuilder.Expression<T>(command.FilterDescriptors);

The command parameter is passed into the AJAX method with the grid filter stings, and Telerik's ExpressionBuilder parses it into an Expression.

Then essentially the app does this:

IQueryable<T> query = session.Query<T>();
if (where != null) query = query.Where(where);
int rowCount = query.Count();

and it's that Count that gives the error. With my limited understanding of what is going on with the expression code, it looks correct to me. The left and right parts of the parsed Boolean expression are:

((IIF((item != null), item.AccountName, null) ?? "").ToLower().StartsWith("abc".ToLower()) == True)
((IIF((item != null), item.AccountName, null) ?? "").ToLower().EndsWith("def".ToLower()) == True)

But somehow, when NH's VisitConstantExpression is called, the _parameters Dictionary already has the ConstantExpression in it, hence the error message.

-glenn-

unread,
Mar 10, 2012, 10:13:30 AM3/10/12
to nhu...@googlegroups.com
Not sure I can post a test, at least not one that can be compiled, because it would require me including a Telerik DLL which I don't think I'm allowed to do. But I've opened an issue in the Telerik forums and will try to get a sample up there.

http://www.telerik.com/community/forums/aspnet-mvc/grid/more-than-one-grid-filter-gives-an-item-with-the-same-key-has-already-been-added.aspx

On Friday, March 9, 2012 4:43:55 PM UTC-5, Oskar Berggren wrote:
2012/3/9 -glenn-:

Alexander I. Zaytsev

unread,
Mar 10, 2012, 12:49:27 PM3/10/12
to nhu...@googlegroups.com
This issue should be fixed in the trunk. Could you check this with latest version? 

суббота, 10 марта 2012 г. 2:21:55 UTC+6 пользователь -glenn- написал:

Oskar Berggren

unread,
Mar 10, 2012, 3:17:25 PM3/10/12
to nhu...@googlegroups.com
Alexander may be correct about this already being fixed in trunk. You
can find continuous builds at teamcity.codebetter.com to test with, or
wait for 3.3.0.CR1 which should be out tomorrow.

/Oskar


2012/3/10 -glenn- <gdo...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nhusers/-/hTFNjagE9LQJ.
>
> To post to this group, send email to nhu...@googlegroups.com.
> To unsubscribe from this group, send email to
> nhusers+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.

Reply all
Reply to author
Forward
0 new messages