Search code examples
c#mongodbienumerablemongodb-.net-drivermongodb-query

How to get MongoCursorEnumerator<T> with latest version C# (1.8.2+) driver?


Having some issues with code which used to work pre-1.8.2 version of C# MongoDB Driver:

With previous releases of the driver, I could do something like:

private MongoCursorEnumerator<T> InitializeCursor()
{
    var cursor = _queue.Find(Query.GT("_id", _lastId));
    return MongoCursorEnumerator<T>cursor.GetEnumerator();
}

Now, this code generates exception:

Unable case type "d__0" to type "MongoDB.Driver.MongoCursorEnumerator`1".

Need the cast to MongoCursorEnumerator because it has the property IsDead which is used later in the code for re-initializing the cursor. Somehow, up-casting to MongoCursorEnumerator no longer works. Anyone know why this is happening or what would be the possible fix to this?


Solution

  • From V1.8 on you have to use the constructor of MongoCursorEnumerator.

    return new MongoCursorEnumerator<T>(cursor);
    

    Unfortunately there is no hint in the documentation - you have to look at the source code :-/