I’m trying to order my results by the value in an embedded document.
Consider a model such as:
public class Car
{
public Guid ID { get; set; }
public string Name { get; set; }
public IEnumerable<Passenger> Passengers { get; set; }
}
public class Passenger
{
public Guid ID { get; set; }
public virtual string Name { get; set; }
public virtual int Age { get; set; }
}
I’m trying to query my Car collection, and order by Passenger.Age
My query looks something like:
var results = (from car in _db.GetCollection<Car>("car").AsEnumerable()
from passenger in car.Passengers
where car.Name == "Ford"
orderby passenger.Age).ToList();
With this, I get the following exception:
The SelectMany query operator is not supported.
This is understandably a limitation of the C# mongo driver.
Is there a workaround?
Failing that, how could I order them after my .ToList() ?
You can probably re-write this with
AsQueryable()to get anIEnumerablecollection fromToList()back, from which you can then further query with any LINQ you want to use, not just the operations directly supported by theMongoCollection:Here’s where you can find the directly supported LINQ operations for the MongoDb C# driver.