[CSHARP-2355] OfType doesn't consider known derived classes Created: 12/Aug/18 Updated: 27/Oct/23 Resolved: 04/Jan/19 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Linq |
| Affects Version/s: | 2.7.0 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Oleksii Bespalov | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | LINQ, OfType, Queryable, question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
For the following inheritance chain:
querying with OfType wih the base type B doesn't consider derived classes added via BsonKnownTypes:
while for regular IEnumerable it does:
|
| Comments |
| Comment by Robert Stam [ 04/Jan/19 ] | |||||||||||||||||||||||||||||||||||||||
|
This is an interesting issue. It raises the question of whether OfType<B> should only match documents of type B or also subclasses of B. I hesitate to change the current behavior of OfType<> because it could be considered a breaking change. If you want of OfType<B> to also match subclasses of B also you could configure your POCOs so that the discriminator is an array of all types (starting from the class identified as the root of the polymorphic hierarchy). You configure the driver to use array discriminators by tagging which class should be considered as the root of the polymorphic hierarchy. So using your example (but changing the Id type to int for convenience):
If you then insert the following documents:
We can see the resulting discriminators using the MongoDB shell:
When the discriminator is configured this way OfType<B> will also match documents of type C because of the way MongoDB handles queries against array fields. Look at these results in the MongoDB shell to see that:
To verify the desired behavior when the disciminators are configure this way (using C# code) try:
a should contain 3 items. b should contain 2 items. c should contain 1 item. Due to the reluctance to make a breaking change to the behavior of OfType<> and the availability of a convenient workaround I'm going to close this as "Works as Designed".
|