Dreaming in Code: Multi Linq
I was asked how we will approach the same Multi Query approach with Linq integration, here are some thoughts about it.
var posts = from post in data.Posts where post.User.Name == "Ayende" orderby post.PublishedDate desc; var postsCount = posts.Count(); posts.Skip(10).Take(15); new LinqQueryBatch() .Add(posts) .Add(postsCount) .Execute();//perform the query foreach(Post p in posts)//no query { Console.WriteLine(p.ToString()); } //no query Console.WriteLine("Overall posts by Ayende: {0}", postsCount.Single() );
The LinqQueryBatch in this case doesn't need to pass delegates to process the results, it can modify the Linq Query directly, so trying to find the result will find the one that was already loaded when we executed the multi query.
Again, this is something that I am merely thinking about, no concrete code created.
Comments
It's a good start.
Hmm... Won't "posts.Count()" force posts to be evaluated? It doesn't just wrap "posts" with a new "IEnumerable<T>" which can be evaluated on demand. It really wants to return an "int" immediately. That's the kind of thing I was worried about with Multi-LINQ.
I suppose we can define Count() in a non-standard way to return an IEnumerable<int> instead. Looks like that's what you're doing up there actually. Just not sure whether it'll play nice with standard LINQ idioms.
(I hope this doesn't turn into another one of those irritating syntax gotchas in C# like with event raising in Rhino.Mocks...)
Jeff, I agree, but I don't see any other option to do it, frankly.
I would have returned an Int32Proxy, but that is not something that is possible :-)
If your Add method took a lambda expression you could have your Count query w/o executing it prematurely.
new LinqQueryBatch()
.Add( () => posts)
.Add( () => posts.Count() )
.Execute();
However, it would possibly cleaner to simply have an execute method that took one or more lambda's to execute.
LinqQueryBatch.Execute( () => posts, () => posts.Count() );
Theoretically, yes.
But consider complex expressions, that is not something that I would like to do inline
Comment preview