-
Type: Bug
-
Resolution: Duplicate
-
Priority: Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Dotnet Drivers
Summary
We used LINQ to "Union" two collections and "Join" in a third one. When we tried that the query throws an error:
System.InvalidOperationException : More than one possible serializer found for <>f__AnonymousType0`2[System.String,System.Boolean] in x.
This is true for versions 2.27.0, 2.26.0, 2.25.0 of the MongoDB.Driver nuget package.
How to Reproduce
write followin statement and try to execute it.
var queryable = _firstCollection .AsQueryable() .Where(c => c.Name.StartsWith("second")) .Union(_secondCollection.AsQueryable() .Where(c => c.Name.StartsWith("another"))) .Where(x => x.Id == 2);
First of all, it's not possible to chain an Where after the Union because it does not return an "IMongoQuerable<T>". This is easy solved with:
public static IMongoQueryable<TSource> Union<TSource>(this IMongoQueryable<TSource> source1, IEnumerable<TSource> source2) { Ensure.IsNotNull(source1, nameof(source1)); Ensure.IsNotNull(source2, nameof(source2)); return (IMongoQueryable<TSource>)Queryable.Union(source1, source2); }
But after that it throws the Exception from above.
I figured out an workaround how to fix it, but I am not able to run the whole set of unit tests that are available in the project, because it seems like it needs an local running mongodb to perform them.
So I don't know if this is a proper fix or there maybe an better solution for this issue.
I will create an PR with what I have and hopefully you can give some feedback on that.
- duplicates
-
CSHARP-5229 Using the same anonymous type twice in a LINQ query throws an exception
- Closed