You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Reusing the ParameterExpression on the left-hand-side of a lambda in an IQueryable chain throws an ArgumentException while parsing the chain. This can be an issue when manipulating LINQ expressions with things like NeinLinq's predicate translator.
Mitigation
Clone any expressions before reuse, or inline them instead.
Illustration
asyncTask<List<Pet>>GetPetsByOwner(IEnumerable<Expression<Func<Person,bool>>>ownerFilters){Expression<Func<Pet,Person>>ownerExpression=pet => pet.Owner;IQueryable<Pet>petCollectionQueryable=/*...*/;foreach(var ownerFilter in ownerFilters){petCollectionQueryable= petCollectionQueryable.Where(ownerFilter.Translate().To(ownerExpression));}returnawait petCollectionQueryable.ToListAsync();}
in this case, the same "pet" parameter expression will be used by all the built filters on petCollectionQueryable (e.g. pet => pet.Owner.Name == "Joe", pet => pet.Owner.Age > 20), unlike if the filters had been written manually. This will cause the exception detailed below in "To Reproduce".
To Reproduce
Navigate to this code in the solution:
System.Linq.Expressions.Expression<Func<Family,bool>>predicate=f => f.Int ==5;
inputs.Add(new LinqTestInput("Where -> OrderBy with same predicate",b => getQuery(b).Where(predicate).OrderBy(predicate)));
Debugging will return this exception:
System.ArgumentException
HResult=0x80070057
Message=An item with the same key has already been added. Key: f
Source=System.Private.CoreLib
StackTrace:
at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[T](T key)
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Microsoft.Azure.Cosmos.Linq.ParameterSubstitution.AddSubstitution(ParameterExpression parameter, Expression with) in C:\_SRC\azure-cosmos-dotnet-v3\Microsoft.Azure.Cosmos\src\Linq\TranslationContext.cs:line 326
Describe the bug
Reusing the
ParameterExpression
on the left-hand-side of a lambda in an IQueryable chain throws anArgumentException
while parsing the chain. This can be an issue when manipulating LINQ expressions with things like NeinLinq's predicate translator.Mitigation
Clone any expressions before reuse, or inline them instead.
Illustration
in this case, the same "
pet
" parameter expression will be used by all the built filters onpetCollectionQueryable
(e.g.pet => pet.Owner.Name == "Joe"
,pet => pet.Owner.Age > 20
), unlike if the filters had been written manually. This will cause the exception detailed below in "To Reproduce".To Reproduce
Navigate to this code in the solution:
azure-cosmos-dotnet-v3/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/LinqGeneralBaselineTests.cs
Lines 1218 to 1220 in 67cbd89
And then stick the following code after it:
Debugging will return this exception:
The full stack trace continues with:
The text was updated successfully, but these errors were encountered: