RavenDB version 2.5. Other versions:

Live Projections

Live Projections feature is obsolete in favor of Result Transformers.

Usually, RavenDB can tell what types you want to return based on the query type and the CLR type encoded in a document, but there are some cases where you want to query on one thing, but the result is completely different. This is usually the case when you are using live projections.

For example, let us take a look at the following index:

    public class PurchaseHistoryIndex : AbstractIndexCreationTask<Order, Order>
    	public PurchaseHistoryIndex()
    		Map = orders => from order in orders
    						from item in order.Items
    						select new
    							UserId = order.UserId,
    							ProductId = item.Id
    		TransformResults = (database, orders) =>
    						   from order in orders
    						   from item in order.Items
    						   let product = database.Load<Product>(item.Id)
    						   where product != null
    						   select new
    							   ProductId = item.Id,
    							   ProductName = product.Name

Note that when we query this index, we can query based on UserId or ProductId, but the result that we get back aren't of the same type that we query on. For that reason, we have the OfType<T> (As<T>) extension method. We can use it to change the result type of the query:

    documentSession.Query<Shipment, PurchaseHistoryIndex>()
    	.Where(x => x.UserId == userId)

In the code above, we query the PurchaseHistoryIndex using Shipment as the entity type to search on, but we get the results as PurchaseHistoryViewItem.

Comments add new comment

The comments section is for user feedback or community content. If you seek assistance or have any questions, please post them at our support forums.

Chris Haines
REPLY Posted by Chris Haines on

This is hard to understand.

Where did Shipment suddenly come from, and how does that relate to the Order class?

What does the PurchaseHistoryViewItem class look like?

Itamar Syn-Hershko
REPLY Posted by Itamar Syn-Hershko on

See a more through discussion here http://ravendb.net/docs/client-api/querying/handling-document-relationships

REPLY Posted by bayger on

Do Live Projections work with Lucene Queries via Advances.LuceneQuery<>? It seems that there is no As<> extension method for IDocumentQuery. How to achieve that functionality there?

Ayende Rahien
REPLY Posted by Ayende Rahien on

You don't need to call As<> there. We only need to do this in the Linq API to convert between the strongly typed options. In LuceneQuery, you can start with the end type from the get go.