I have a MongoDB database with a few collections each of which stores objects of a specific type. I am trying to implement a generic selection function to operate on a specific collection depending on the type, like in the following definition:
object[] Select<T>(Func<T, bool> condition)
E.g., if one of the object types is a Person class, I would implement the following:
object[] Select<T>(Func<T, bool> condition)
{
if (typeof(T) == typeof(Person))
{
Func<Person, bool> f = (Person p) =>
{
return true;
};
return this.collectionPersons.AsQueryable().Where(p=>f(p)).ToArray();
}
else // ...
}
This code compiles, but when I try to run it, I get a System.ArgumentException with the
Additional information: Unsupported filter:
Invoke(value(System.Func`2[Person,System.Boolean]), {document}).
After perusing the API documentation I have the impression that it is generally not possible to use a lambda expression of abstract kind (like in the above example), but only those supported by
FilterDefinitionBuilder such as Gt(), Eq() etc.
I am curious if I understand this correctly or there does exist a possibility to query a collection with an abstract predicate (I am quite new to MongoDB C# driver).