Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:14:53+00:00 2026-06-04T12:14:53+00:00

I have this logic in an extension method that creates a paging wrapper: int

  • 0

I have this logic in an extension method that creates a paging wrapper:

        int total = 0;
        if (query is IRavenQueryable<T>)
        {
            RavenQueryStatistics stats;
            var rQuery = (IRavenQueryable<T>)query;
            rQuery.Statistics(out stats);
            total = stats.TotalResults;
        }
        else
        {
            total = query.Count();
        }

My problem is that when I do a raven query, my stats.TotalResults is zero, unless I hover over rQuery during debug and try to expand the results.

What am I doing wrong here? How to I get raven to consistently give me the results I need?

Here is the whole method:

    public static PagingModel<T> Page<T>(this IEnumerable<T> query, int page, int count)
    {
        if (page < 1)
            throw new ArgumentOutOfRangeException("Page is one-based and must be greater than zero");

        int total = 0;
        if (query is IRavenQueryable<T>)
        {
            RavenQueryStatistics stats;
            var rQuery = (IRavenQueryable<T>)query;
            rQuery.Statistics(out stats);
            total = stats.TotalResults;
        }
        else
        {
            total = query.Count();
        }

        var results = query
            .Skip((page - 1) * count)
            .Take(count)
            .ToArray();

        return new PagingModel<T>()
        {
            Page = page,
            Rows = results,
            Total = total /count
        };
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:14:55+00:00Added an answer on June 4, 2026 at 12:14 pm

    I figured it out myself. It turns out that RavenQueryStatistics sets the TotalResults by reference and the total is only available after the results have been resolved. (I originally thought it made a separate trip to the DB.)

        public static PagingModel<T> Page<T>(this IEnumerable<T> query, int page, int count)
        {
            RavenQueryStatistics stats = null;
            if (page < 1)
                throw new ArgumentOutOfRangeException("Page is one-based and must be greater than zero");
    
            if (query is IRavenQueryable<T>)
            {
                var rQuery = (IRavenQueryable<T>)query;
                rQuery.Statistics(out stats);
            }
    
            var results = query
                .Skip((page - 1) * count)
                .Take(count)
                .ToArray();
            var total = stats == null ? query.Count() : stats.TotalResults;
            return new PagingModel<T>()
            {
                Page = page,
                Rows = results,
                TotalPages = (int)Math.Ceiling((double)total / (double)count)
            };
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with the logic of this code, XNA's Update Method is
I want to have an extension method for XElement/XAttribute that allows me to apply
I have this piece of logic I would like to implement as a trigger,
i have this code, and need help with the logic end. I would like
I'm trying to figure out the logic of how to do this. I have
I have this horribly stripped delphi code that basically login to server, save cookie
I have this table: User id INT PK login VARCHAR UNIQUE I want to
my first question is, where do I put this custom extension so that it
I have run into this problem: Custom Methods & Extension Methods cannot be translated
I have a list that contains FrameworkElements and I want to create an extension

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.