Given a simple model:
public class Model{
private string _id;
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[BsonRepresentation(BsonType.ObjectId)]
public string Id {
get { return _id; }
set { _id = value; }
}
//Additional fields
}
Assume I want to select only a few fields from this class based off a query:
_collection.AsQueryable<Model>().Where(m => m.Id == id)
.Select( x =>
new Model(){
Id = x.Id,
SomeField = x.SomeField
});
The following error will occur: Element 'Id' does not match any field or property of class Model.
If there is no select clause, the Id is set and is returned as expected. Also, if you remove the field Id from the select, it will return everything as expected except the Id will be null.
We want to use the select to limit the amount of data returned. We could use a dynamic object (or create a new object to pass around), but seems to be redundant.
Any suggestions to this issue?