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

  • SEARCH
  • Home
  • 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 6000185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:39:27+00:00 2026-05-23T00:39:27+00:00

Im getting a paged datasource from a fairly complex linq query. My problem is

  • 0

Im getting a paged datasource from a fairly complex linq query. My problem is that is takes twice as long to execute since I need to get the total row count before paging is applied in order to calculate the nr. of pages to dispaly. (the query will be executed twice)

Is there somehow I can do this in a more optimal way? Like using SQL’s @@rowcount somehow?

This is roughly what the query looks like right now. (using Dynamic linq)

    public IList<User> GetPagedUsers(string filter, string sort, int skip, 
       int take, out int totalRows)
    {
            using(var dbContext = new DataContext())
            {
               IQueryable<User> q = GetVeryComplexQuery(dbContext);

               //Apply filter if exists
               if (!string.IsNullOrEmpty(filter))
                   q = q.Where(filter);

               //Set total rows to the out parameter
               totalRows = q.Count(); //Takes 4 sec to run

               //Apply sort if exists
               if (!string.IsNullOrEmpty(sort))
                   q = q.OrderBy(sort);

               //Apply paging and return
               return q.Skip(skip).Take(take).ToList(); //Takes 4 sec to run
            }
    }

Why wont this for example work?

TblCompanies.Dump(); //150 rows
ExecuteQuery<int>("select @@ROWCOUNT").Dump(); //returns 0
  • 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-05-23T00:39:28+00:00Added an answer on May 23, 2026 at 12:39 am

    Linq2Sql will actually translate the use of Skip & Take into the SQL Statement so even if you could get @@RowCount the value will not be great than your take parameter.

    If we take the following simple example (lifted from MSDN http://msdn.microsoft.com/en-us/library/bb386988.aspx).

    IQueryable<Customer> custQuery3 =
        (from custs in db.Customers
         where custs.City == "London"
         orderby custs.CustomerID
         select custs)
        .Skip(1).Take(1);
    
    foreach (var custObj in custQuery3)
    {
        Console.WriteLine(custObj.CustomerID);
    }
    

    the following SQL is generated

    SELECT TOP 1 [t0].[CustomerID], [t0].[CompanyName],
    FROM [Customers] AS [t0]
    WHERE (NOT (EXISTS(
        SELECT NULL AS [EMPTY]
        FROM (
            SELECT TOP 1 [t1].[CustomerID]
            FROM [Customers] AS [t1]
            WHERE [t1].[City] = @p0
            ORDER BY [t1].[CustomerID]
            ) AS [t2]
        WHERE [t0].[CustomerID] = [t2].[CustomerID]
        ))) AND ([t0].[City] = @p1)
    ORDER BY [t0].[CustomerID]
    

    So you can see that the skip is actually occurring inside the SQL Statement and therefore @@RowCount is going to equal the amount of rows returned by the query and not the entire result set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i want to add a datarow that im getting back from a data
I have a problem with getting my pager-function to work. For some reason it
I am getting the following error when I post back a page from the
I'm getting some Xml back from a service. I would like it to be
I have a problem with Gridview sorting that is similar to others but I'm
I'm trying to run a stored procedure from my website that disables a trigger.
From everything I've read, it seemed that adding paging to a ListView control should
I have a form that will be populated by a bunch of information from
I have a json object that I'm loading from wordpress using the JSON API
My current best code is: string delNonQuery = DELETE FROM + Settings.DataSource + WHERE

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.