Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top