TermsPartHandler - An item with the same key has already been added.

Topics: Core, Troubleshooting
Developer
Jun 20, 2014 at 8:18 PM
I'm trying to debug part of the TermsPartHandler, but I dont know enough about what it is doing to correctly analyze. And I get the impression that I have a duplicate row in my database somewhere.
part._termParts = new LazyField<IEnumerable<TermContentItemPart>>();
part._termParts.Loader(value => {
    var ids = part.Terms.Select(t => t.TermRecord.Id);
    var terms = _contentManager.GetMany<TermPart>(ids, VersionOptions.Published, queryHint)
        .ToDictionary(t => t.Id, t => t);
    return
        part.Terms.Select(
            x =>
                new TermContentItemPart {
                    Field = x.Field,
                    TermPart = terms[x.TermRecord.Id]
                }
            );
});
2014-06-20 19:07:05,060 [67] Orchard.DisplayManagement.Implementation.DefaultDisplayManager - ArgumentException thrown from Action`1 by System.Action`1[[Orchard.DisplayManagement.Implementation.ShapeDisplayingContext, Orchard.Framework, Version=1.8.1.0, Culture=neutral, PublicKeyToken=null]]
System.ArgumentException: An item with the same key has already been added.
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Orchard.Taxonomies.Handlers.TermsPartHandler.<>c__DisplayClass1b.<InitializerTermsLoader>b__15(IEnumerable`1 value) in \src\Orchard.Web\Modules\Orchard.Taxonomies\Handlers\TermsPartHandler.cs:line 88
   at Orchard.ContentManagement.Utilities.LazyField`1.GetValue() in \src\Orchard\ContentManagement\Utilities\LazyField.cs:line 24
   at Orchard.ContentManagement.Utilities.LazyField`1.get_Value() in \src\Orchard\ContentManagement\Utilities\LazyField.cs:line 10
   at Orchard.Taxonomies.Models.TermsPart.get_TermParts() in \src\Orchard.Web\Modules\Orchard.Taxonomies\Models\TermsPart.cs:line 14
   at Orchard.Taxonomies.Shapes.<Discover>b__15(ShapeDisplayingContext displaying) in \src\Orchard.Web\Modules\Orchard.Taxonomies\Shapes.cs:line 80
   at Orchard.DisplayManagement.Implementation.DefaultDisplayManager.<>c__DisplayClassc.<Execute>b__3(Action`1 action) in \src\Orchard\DisplayManagement\Implementation\DefaultDisplayManager.cs:line 74
   at Orchard.InvokeExtensions.Invoke[TEvents](IEnumerable`1 events, Action`1 dispatch, ILogger logger) in \src\Orchard\InvokeExtensions.cs:line 17
; TraceSource 'w3wp.exe' event
I can fix the issue by turning part.Terms.Select(t => t.TermRecord.Id) into part.Terms.Select(t => t.TermRecord.Id).Distinct(), but I get the impression I'm sweeping a problem under the rug.
Coordinator
Jun 25, 2014 at 8:43 PM
I did change that code, yes, but for an unrelated reason: to remove a select n+1. Adding distinct shouldn't hurt. Go for it.