I am just getting started with RavenDB and have run into a weird scenario.
When I run the following query, the model gets populated just fine. Everything works great.
var contacts = Session.Query<Contact>()
.Where(c => c.UserId == this.userId)
.ToList();
var model = contacts.Select(c => new SelectListItem() {
Text = c.FullName,
Value = c.Id }).ToList();
However, that isn’t the code I started with. I started with the code below which populates the Text property from the contact FullName. For some random reason, it doesn’t populate the Value property from the contact id.
var model = (from c in Session.Query<Contact>()
where c.UserId == this.userId
select new SelectListItem() {
Text = c.FullName,
Value = c.Id }).ToList();
I am not sure if this is a bug or if I am just missing something simple. Ideas?
** UPDATE **
It doesn’t like this syntax either. I must be missing something really basic here.
var model = Session.Query<Contact>()
.Where(c => c.UserId == this.userId)
.Select(c => new SelectListItem() { Text = c.FullName, Value = c.Id })
.ToList();
The difference is that in the first case, you’ve got a
ToList()call before the projection. So the only thing that the LINQ provider needs to worry about is theWherecall. It will fetch all of the data for eachContact, and then the projection occurs in LINQ to Objects instead.In your second code, the
Selectcall needs to be handled by the LINQ provider as well – and presumably it’s not doing the right thing.So yes, this sounds like a bug in the RavenDB LINQ provider, in the
Selecthandling.Note that another way of forcing the rest of the query to execute in LINQ to Objects is to use
AsEnumerable()– so this should work:(I’ve converted
new SelectListItem()tonew SelectListItemin the object initializer expression too, but that’s really a no-op.)