Deferred Execution?

Mar 19, 2011 at 5:12 AM

Does sterling support deferred execution? I wanted to do something like this but it gives me a null reference exception on the return line

 

            var items = (from i in App.Database.Query<Items, Guid>()
                              select i);
            if (items.Count() == 0)
                SaveDefault();

            return items.FirstOrDefault().LazyValue.Value;

 

This would always give an exception the first time you start the emulator, after that their are already items in the database. The only way around this i could see is to do this

 

            var itemsCount = (from i in App.Database.Query<Items, Guid>()
                                   select i).Count();
            if (itemsCount == 0)
                SaveDefault();

            var items = (from i in App.Database.Query<Items, Guid>()
                              select i);
 
            return items.FirstOrDefault().LazyValue.Value;

It even fails if you move the items definition above the SaveDafults method call.

 

Coordinator
Mar 19, 2011 at 10:55 AM

I'll have to look into that specific example. Sterling supports 100% LINQ to objects out of the box. Keep in mind however that when you query, you receive a copy of the index list, not the internal list because manipulating that would cause corruption in the database. I suspect the first call executes the list and the second works against the same (empty) list, and this is a side effect of how the deferred execution works, not Sterling. I'll look into it more closely though and see if there is anything to tweak.

Any reason you are checking for Count() == 0 instead of using .Any()?

 

Mar 19, 2011 at 1:22 PM

to be honest i didn't know about .Any(). So if you wanted to apply filtering to a query on the fly would you possibly counter the same issue? I guess normally you would start with the full data set rather than exclude parts so maybe you wouldn't get an issue. 

I always thought linq would only retrieve the data when you try to access it and because of this the results can change