Home » c# » c# – LinqPad Exception for a basic query: An item with the same key has already been added

c# – LinqPad Exception for a basic query: An item with the same key has already been added

Posted by: admin February 21, 2020 Leave a comment

Questions:

I’m connecting to an Oracle DB using LinqPad, and for the most part, it’s working fine, but for certain tables I keep getting the error message:

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

I understand well enough the concept of distinct keys in a dictionary; what I don’t understand is how this requirement is being violated here.

As an example, I can run the following as SQL from LinqPad without any problems:

Select * from table_name where rownum < 10

..but if I try to run this as a C# Expression, then I get the error above.

TableNames.Take(10);

Just to be clear: OtherTables.Take(10); works fine for many other tables; the above error only occurs for a subset of them.

Preliminary theories

  • I initially thought this might have something to do with a column in the table having a name that conflicts with the table name.

    Example: A table table_name containing a column table_name_s would be mapped to a Linq entity TableNames with a property TableNameS. I thought there might be some conflict between TableNames and TableNameS internally in LinqPad here, but this is at least not a consistent error, as there are other tables with the same naming schemes, which do not cause such errors.

  • Another theory is that this may somehow be related to the driver I’m using in order to connect to Oracle, but this is only a hunch (I’m using the custom “IQ” driver, which supports Oracle, MySql and SQLite).

Any ideas about how to resolve and avoid this error, and be able to access the table contents using Linq?


Update: The stack trace from LinqPad, as requested.

As for the other comment: As I previously noted, I understand the root cause (a duplicate key value), but not where it is coming from. The expression SomeTable.Take(10);is essentially the whole query, only with a different table name.

ved System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
ved System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
ved System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
ved System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector)
ved IQToolkit.Data.Mapping.AttributeMapping.AttributeMappingEntity..ctor(Type elementType, String tableId, Type entityType, IEnumerable`1 attrs, IEnumerable`1 mappingMembers)
ved IQToolkit.Data.Mapping.AttributeMapping.CreateEntity(Type elementType, String tableId, Type entityType)
ved IQToolkit.Data.Mapping.AttributeMapping.GetEntity(Type elementType, String tableId, Type entityType)
ved IQToolkit.Data.Mapping.AttributeMapping.GetEntity(Type type, String tableId)
ved IQToolkit.Data.EntitySession.GetTable(Type elementType, String tableId)
ved IQToolkit.Data.EntitySession.GetTable[T](String tableId)
ved IQDriver.IQContextBase.GetTable[T](String name)
ved LINQPad.User.TypedDataContext.get_Phases()
ved UserQuery.RunUserAuthoredQuery() i C:\Users\MyUserName\AppData\Local\Temp\LINQPad5\_jpjdzkge\query_gktujz.cs:linje 36
ved LINQPad.ExecutionModel.ClrQueryRunner.Run()
ved LINQPad.ExecutionModel.Server.RunQuery(QueryRunner runner)
How to&Answers: