[CSHARP-505] System.MissingMethodException on enumerating IQueryable<Abstract> Created: 20/Jun/12 Updated: 28/Apr/15 Resolved: 28/Apr/15 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.4.2, 1.5 |
| Fix Version/s: | 2.1 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Zaid Masud | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | linq, linq,query | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Epic Link: | Rewrite Linq |
| Description |
|
Consider this example, where Concrete represents a collection and happens to derive from Abstract, which is an abstract base class. IQueryable<Concrete> concretes = collection.AsQueryable(); var queryResults = abstracts.Where(...).ToList(); // results in MissingMethodException System.MissingMethodException: Constructor on type 'MongoDB.Driver.Linq.Projector2[[[MongoDB.Bson.ObjectId, MongoDB.Bson, Version=1.5.0.26036, Culture=neutral, PublicKeyToken=f686731cfb9cc103]]' not found. at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at MongoDB.Driver.Linq.SelectQuery.Execute() at MongoDB.Driver.Linq.MongoQueryable1.GetEnumerator() Note – this used to work perfectly in Fluent mongo. |
| Comments |
| Comment by Craig Wilson [ 28/Apr/15 ] |
|
This was fixed as part of |
| Comment by Craig Wilson [ 28/Jun/12 ] |
|
This already works (or will work with the release of 1.5). You can see the tests here: https://github.com/mongodb/mongo-csharp-driver/blob/master/DriverUnitTests/Linq/SelectOfTypeHierarchicalTests.cs. In addition, as is also indicated in the tests, you can use the OfType<T> operator. I'm going to mark this feature request for 2.0 as there is already a decent workaround for this issue. |
| Comment by Zaid Masud [ 20/Jun/12 ] |
|
Good point, are there any plans for supporting type discriminators with LINQ? For example, rather than the following: var query = Query.EQ("_t", "Cat"); I would love to write: collection.AsQueryable().Where(d => d is Cat); And I'm sure much could be made of the Cast<> LINQ extension method as well. |
| Comment by Craig Wilson [ 20/Jun/12 ] |
|
I agree with your analysis of what you are wanting. However, we do support type discriminators so descendant classes of Abstract could exist in the same collection. This is actually going to take some where I think. |
| Comment by Zaid Masud [ 20/Jun/12 ] |
|
It should return only concretes typed as Abstract. I don't see how it would be possible to get back any type that descends from Abstract, as each type would be represented as a separate collection? I would like to see an exception if the IQueryable<Abstract> contains multiple types in it, although I cannot think of a possible way that such an IQueryable could be constructed. |
| Comment by Craig Wilson [ 20/Jun/12 ] |
|
In your mind, what should this be doing? When you finally execute abstracts.ToList(), should you only get back concretes or would you get back any type that descends from Abstract? |