Mapping a backing field for a collection using the C# driver doesn't work with Linq.

389 views
Skip to first unread message

Jereme Evans

unread,
Feb 13, 2014, 7:49:17 PM2/13/14
to mongod...@googlegroups.com
Hi,

I have a class that has a readonly IEnumerable<> property. There is a backing field that contains the data. When I map the field and name is as the IEnumberable property, I am unable to perform Linq queries against my data. Am I doing something wrong? Quick and dirty example:

class Foo {
  private IList<string> _bars = new List<string>();
  public IEnumerable<string> Bars { get { return new ReadOnlyCollection<string>(_bars); } }
}

Mappings:

            BsonClassMap.RegisterClassMap<Foo>(cm => {

                cm.AutoMap();

                cm.MapField("_bars").SetElementName("Bars");

            });


And when I try and query it:

var collection = _database.GetCollection<Foo>("Foos");

var test = collection.AsQueryable().Any(f => f.Bars == "WTF!!");


I receive this lovely error:

"Class Foo does not have a member called Bars.
Parameter name: memberName"

I would very much expect this to work. It saves the data as expected. Is there a way to do this? If not, when will it be implemented?

Thanks!!

craiggwilson

unread,
Feb 13, 2014, 9:25:07 PM2/13/14
to mongod...@googlegroups.com
This is a very interesting problem.  You are accessing the member "Bars" and the class map doesn't think it has been mapped (which it hasn't).  There isn't a current way to link two members together.  Feel free to file a Jira ticket for the CSHARP project if you think this would be nice to have (which you obviously do).  http://jira.mongodb.org

The best way to workaround this right now would be to create a private setter on the Bars property and map it instead of _bars.
Reply all
Reply to author
Forward
0 new messages