Brett Allred
Brett Allred

Reputation: 3487

Raven Db with different LINQ syntax doesn't appear to be working

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();

Upvotes: 3

Views: 235

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500525

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 the Where call. It will fetch all of the data for each Contact, and then the projection occurs in LINQ to Objects instead.

In your second code, the Select call 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 Select handling.

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:

var model = Session.Query<Contact>()
                   .Where(c => c.UserId == this.userId)
                   .AsEnumerable()
                   .Select(c => new SelectListItem { 
                                  Text = c.FullName, 
                                  Value = c.Id })
                   .ToList();

(I've converted new SelectListItem() to new SelectListItem in the object initializer expression too, but that's really a no-op.)

Upvotes: 2

Related Questions