"System.ArgumentException: An item with the same key has already been added." while trying to sort LINQ query in memory by LINQ association

We are querying against a collection of LINQ object data in memory. The wrinkle is that we order a column in a linked table whose records have not yet been loaded (deferred loading :)

Dim oPkgProducts = _
From b In oBillPkg.BillProducts _
Where b.Successful.GetValueOrDefault(Common.X_INDETERMINATE) = _
    Common.X_INDETERMINATE _
Order By _
    b.ProductFromCache.ProvisionCodePriority.ProvisionPriority Ascending _
Select b

We made the following error when creating the code when the code tried to call Count () in this query:

System.ArgumentException: An item with the same key has already been added. in System.ThrowHelper.ThrowArgumentException (ExceptionResource resource) in System.Collections.Generic.Dictionary 2.Insert(TKey key, TValue value, Boolean add) at System.Data.Linq.IdentityManager.StandardIdentityManager.SetCurrent(MetaType type) at System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues) at System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues) at System.Data.Linq.CommonDataServices.DeferredSourceFactory1.TryGetCached Object (Object [] keyValues, T & cached) in System.Data.Linq.CommonDataServices.DeferredSourceFactory 1.Execute(Object instance) at System.Data.Linq.CommonDataServices.DeferredSourceFactory1.DeferredSource.GetEnume System.Linq.Enumerable.SingleOrDefault [TSource] (IEnumerable 1 source) at System.Data.Linq.EntityRef1.get_Entity () in PackageManagement.Product.get_ProvisionCodePriority () in C: \ Projects \ DLLs \ DotNet35 \ PackageManagement \ PackageManagement.designer.vb: line 14431 on ProvisionEagePro.BillPackPackPackPackPack .Lambda $ _12 (BillProduct b) in C: \ Projects \ DLL \ DotNet35 \ ProvisionEngine \ BillPackageProvision.vb: line 394 on System.Linq.EnumerableSorter2.ComputeKeys(TElement[] elements, Int32 count) at System.Linq.EnumerableSorter1.Sort (TElement [] elements, number of Int32) under System.Linq.OrderedEnumerable 1.<GetEnumerator>d__0.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext () in System.Linq.Enumerable.Count [TSource] (IEnumerable`1 source) in ProvisionEngine.BillPackageProvision.ProvisionPackage (BillPackage oBillPkg, Boolean bRecursiveCall) in C: \ Projects \ DLL \ DotNet35 \ ProvisionEngine \ BillPackageProvision.vb: line 397

The confusing part is that when the operation was automatically repeated a few seconds later, she succeeded. And since this is the only time we saw an exception (the code works fine the rest of the time), it will not be easy to reproduce.

: "ProductFromCache" - , Pete Montgomery LINQ to SQL. , LINQ.Product . , , , .NET System. , , .ProvisionCodePriority - , . , LINQ Dictionary. - .

- , ?

+3
1

, :

    Dim oPkgProductsCount = (From b In oBillPkg.BillProducts _
Where b.Successful.GetValueOrDefault(Common.X_INDETERMINATE) = _
    Common.X_INDETERMINATE _
Order By _
    b.ProductFromCache.ProvisionCodePriority.ProvisionPriority Ascending _
Select b).count

Dim oPkgProducts = (From b In oBillPkg.BillProducts _
Where b.Successful.GetValueOrDefault(Common.X_INDETERMINATE) = _
    Common.X_INDETERMINATE _
Order By _
    b.ProductFromCache.ProvisionCodePriority.ProvisionPriority Ascending _
Select b).ToList()
dim count = oPkgProducts.count
0

Source: https://habr.com/ru/post/1788290/


All Articles