Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,124 | Comments: 45,475

filter by tags archive

Dreaming in Code: Multi Linq

time to read 1 min | 137 words

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

new LinqQueryBatch()
 .Execute();//perform the query

foreach(Post p in posts)//no query
//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.


Jeff Brown

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" 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 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...)

Ayende Rahien

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 :-)

Matt Warren

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


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

Ayende Rahien

Theoretically, yes.

But consider complex expressions, that is not something that I would like to do inline

Comment preview

Comments have been closed on this topic.


  1. RavenDB 3.5 whirl wind tour: You want all the data, you can’t handle all the data - one day from now
  2. The design of RavenDB 4.0: Making Lucene reliable - about one day from now
  3. RavenDB 3.5 whirl wind tour: I’ll find who is taking my I/O bandwidth and they SHALL pay - 3 days from now
  4. The design of RavenDB 4.0: Physically segregating collections - 4 days from now
  5. RavenDB 3.5 Whirlwind tour: I need to be free to explore my data - 5 days from now

And 14 more posts are pending...

There are posts all the way to May 30, 2016


  1. RavenDB 3.5 whirl wind tour (14):
    29 Apr 2016 - A large cluster goes into a bar and order N^2 drinks
  2. The design of RavenDB 4.0 (13):
    28 Apr 2016 - The implications of the blittable format
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats