Complex Linq queries in RavenDB 4.0
With RQL in RavenDB 4.0, we have a whole new set of capabilities during queries. In the RC1 release of RavenDB we had limited support for expressing the more complex queries in a strongly typed fashion using Linq. If you needed more complex queries, you had to drop to RQL strings. With the RC2 release, we bring you strongly typed and quite powerful queries using Linq. Let us see how this looks like for a very simple query:
This isn’t really interesting, but it does show that we can do projections directly from the server side. Let us see something a bit more complex, shall we?
This query is more interesting, because you can see that we are doing something that you previously had to create a transformer for. We are loading a related documents during the query and projecting a field from it back to the user.
In fact, you can even go crazy and define even more complex behavior in the query:
In this case, we load two related documents and define a formatting function for names, which we can then apply on two documents.
This kind of code eliminates the need to have transformers entirely, and this makes me very happy.
Comments
Why do the double quotes around the literal space need to be escaped in the second example, but not in the first and third?
Aleksander, You shouldn't, I copied it from elsewhere and didn't notice it.
This makes joining related data more natural from a query perspective. I like it. Transformers were great, but this is better.
For async session. How do we perform load syntax? Will that be look like following?
You can user RavenQuery.Load<>(), or be creative and start a sync OpenSession with it ;) and use the session.Load from that. It will make ayende cry though so I should stick with the static method.
Jaon,
RavenQuery.Load
can be used in async context to handle that.It was possible to use transformers with session.Load<TTransformer,TResult>.. Since they are gone now, what is the best way to project the result of load operation?
Andy, This has been replaced with a query that check the id. We have optimized that server side so it is not using any indexes but load the document directly, but it is much nicer to write.
Is there an easy way to get the query executed by raven db based on the IQueryable?
Mathia, You can call
.ToString()
on themComment preview