views:

135

answers:

2

For background, I am using MongoDB and Rob Conery's linq driver. The code I am attempting is thus:

using (var session = new Session<ContentItem>())
{
    var contentCollection = session.QueryCollection.Where(x => x.CreatedOn < DateTime.Now).OrderByDescending(y => y.CreatedOn).ToList();
    ViewData.Model = contentCollection;
}

this will work on one machine, but on another machine I get back no results. To get results i have to do

using (var session = new Session<ContentItem>())
{
    var contentCollection = session.QueryCollection.Where(x => x.CreatedOn < DateTime.Now).ToList();
    ViewData.Model = contentCollection.OrderByDescending(y => y.CreatedOn).ToList();
}

I have to do ToList() on both lines, or no results. If I try to chain anything it breaks. This is the same project, all dll's are locally loaded. Both machines have the same framework, versions of Visual studio and addons. the only difference is one has VisualSVN the other AnkhSVN. I can't see those causing the problem.

Also, while debugging, on the machine that does not work you can see the items in the collection, and if you remove ordering all together it will work. This has got me completely stumped.

A: 

It sounds suspiciously like a good old-fashioned bug in the MongoDB LINQ provider to me. What happens if you try the code below? Does it return all items in reverse date order? If so, it could be that the LINQ provider simply doesn't support complicated queries.

using (var session = new Session<ContextItem>())
{
    var allByDateReverse =
        session.QueryCollection.OrderByDescending(x => x.CreatedOn);
    ViewData.Model = allByDateReverse.ToList();
}
Damian Powell
Thanks for the suggestion. The provider does support the query as it does work on the first machine. It is my second dev machine in which the query is not working. As I stated all the code is the same, pulled from the same svn repository.
Jeremy B.
A: 

Whats the content of x.CreatedOn ? The query

Where(x => x.CreatedOn < DateTime.Now)

is not executed until the contentCollection is actually used. In the second example the ToList() causes the query to be executed immediately while the combined Where(..).OrderByDescending(..) may be executed later (don't know what the ViewData.Model does). May have to do with some weird timing issues there.