views:

71

answers:

3

I have a Linq query as

var mdls = (from mdl in query dbSession.Query<MyModel>("MyIndex")
              orderby mdl.Name
              select dept).Skip(page.Value).Take(4);

Where "MyIndex" is a simple index defined in RavenDB. I know that while querying an Index in RavenDB it returns "TotalResults". See here

How can i get the query result which has the TotalResult property?

A: 

You need to do something like this at the end of your query

.Customize(x => x.TotalResult)

The TotalResult property is only available on the LuceneQuery, not the LINQ Query.

Matt Warren
@Matt I didn't find `x.TotalResult` property. can you please show me the complete LINQ statement.
ajay_whiz
Sorry I missed a bit out, see the docs for the full syntax, http://ravendb.net/faq/total-results-in-paged-data-set
Matt Warren
@Matt there it talks about `session.LuceneQuery<T>` but here I am using `session.Query<T>`
ajay_whiz
+2  A: 

If you are executing a LuceneQuery you get a DocumentQuery returned that has a QueryResult property that contains TotalResults so you can access it as follows:

var documentQuery = (from mdl in query dbSession.LuceneQuery<MyModel>("MyIndex")
                     orderby mdl.Name
                     select dept).Skip(page.Value).Take(4);

var totalResults = documentQuery.QueryResult.TotalResults;

If you're executing a LINQ Query instead then you can call Count() on the query before limiting it with Skip and Take:

var linqQuery = (from mdl in query dbSession.Query<MyModel>("MyIndex")
                      orderby mdl.Name
                      select dept);

var totalResults = linqQuery.Count();

var pageOfResults = linqQuery.Skip(page.Value).Take(4);
Adam
@Adam I get `QueryResult` if i do `LuceneQuery<Department>` but not in the above case.
ajay_whiz
@ajay Good point updating my answer
Adam
A: 

It seems that you can get QueryResult through session.LuceneQuery<YouModel>("YourIndex") and not from session.Query<YourModel>("YourIndex"). But, I wonder why would one use session.Query<YourModel>("YourIndex") ?

ajay_whiz