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 659767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:04:54+00:00 2026-05-13T23:04:54+00:00

I have a problem on using LinqToSQL with paging + dynamic sorting. These are

  • 0

I have a problem on using LinqToSQL with paging + dynamic sorting. These are my sample code.

Using db As New MyDataContext(connectionString)
      db.Log = new DebuggerWritter
      Dim result = db.User.OrderBy(Function(u) u.UserId)

      result = result.Skip((pageNo - 1) * pageSize).Take(pageSize)      
End Using

This is the SQL script generated by LINQToSQL, which is only retrieve certain row of records.

SELECT [t1].[UserId], [t1].[UserName]
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY [t0].[UserId]) AS [ROW_NUMBER], [t0].[UserId], [t0].[UserName]
    FROM [dbo].[User] AS [t0]
    ) AS [t1]
WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p0 + @p1
ORDER BY [t1].[ROW_NUMBER]

But if i implemented dynamic sort,

Using db As New MyDataContext(connectionString)
      db.Log = new DebuggerWritter
      Dim result = db.User

      For Each s In sortExpressions

          Dim expression As Func(Of User, Object) = Function(u) u.[GetType]().GetProperty(s.propertyName).GetValue(u, Nothing)

          Select Case s.SortOrder
               Case SortExpression.SortDirection.Ascending
                    result = result.OrderBy(expression).AsQueryable
               Case SortExpression.SortDirection.Descending
                    result = result.OrderByDescending(expression).AsQueryable
          End Select
      Next

      result = result.Skip((pageNo - 1) * pageSize).Take(pageSize)  
End Using

This is the SQL script generated this time,

SELECT [t0].[UserId], [t0].[UserName]
FROM [dbo].[User] AS [t0]

How come the paging control script is not generated and the sorting is gone?? Is that the way i implement dynamic sorting is wrong? or LinqToSQL did not support paging + dynamic sorting??
Help!!!

  • 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-13T23:04:55+00:00Added an answer on May 13, 2026 at 11:04 pm

    What you’re doing in your code will not work. However, I’m a bit surprised that LINQ to SQL doesn’t throw an exception.

    The OrderBy method that you call in result.OrderBy(expression), is the Enumerable.OrderBy method and not the Queryable.OrderBy method. You already noticed this, because the object it returns is an IEnumerable(Of User) and not an IQueryable(Of User). For this reason you are converting it to an IQueryable(Of User) by calling AsQueryable(). Calling AsQyeryable however, cannot work. The reason for this is that LINQ to SQL processes expression trees (what an IQueryable basically is). And expression trees can be dynamically combined, as you already do with result = result.Skip. Enumerable however, doesn’t work with expression trees, but with delegates, which are compiled method calls. While you can convert such a compiled method call to an IQueryable, LINQ to SQL cannot analyze it, because it still is compiled code (it would take introspection to do this, which only specialized tools as Reflector support). In this case LINQ to SQL probably ignored the complete order by part of your query, because it found a (compiled) method call it couldn’t process.

    So, you’re solution will only work when you do the following:

    Dim expression As Expression(Of Func(Of User, [ExpectedKeyHere]) = _
       ...  creation of the expression here ...
    
    Select Case s.SortOrder
        Case SortExpression.SortDirection.Ascending
            result = result.OrderBy(expression)
        Case SortExpression.SortDirection.Descending
            result = result.OrderByDescending(expression)
    End Select
    

    There is however -yet- another thing. You seem to loop through a set of sortExpressions. While it is possible to append an already sorted collection, an already sorted collection must be called with result.ThenBy or result.ThenByDescending. Calling it with result.OrderBy will completely resort it; you will loose the initial sorting order.

    To make a long story short, perhaps you should try to build some sort of EntitySorter object that allows callers of your method to specify the sort order. Your service method could than look like this (sorry, it’s C#):

    public static Person[] GetAllPersons(IEntitySorter<Person> sorter)
    {
        Condition.Requires(sorter, "sorter").IsNotNull();
    
        using (var db = ContextFactory.CreateContext())
        {
            IOrderedQueryable<Person> sortedList =
                sorter.Sort(db.Persons);
    
            return sortedList.ToArray();
        }
    }
    

    I’ve written a blog about exactly this, again, it’s C#, but I think it will do exactly what you need.

    Good luck.

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

Sidebar

Ask A Question

Stats

  • Questions 327k
  • Answers 327k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use remote_form_for to submit the form in the background via… May 14, 2026 at 1:50 am
  • Editorial Team
    Editorial Team added an answer The Size Of Image is, according to dumpbin 890, while… May 14, 2026 at 1:50 am
  • Editorial Team
    Editorial Team added an answer The only command which could come close of what you… May 14, 2026 at 1:50 am

Related Questions

I have tried some ways to use LINQ dynamic queries - LINQKit and LINQ
I have been puzzling over a problem this morning with LinqToSQL. I'll try and
I have a LinqDataSource that I use to calculate the number of rows in
LINQToSQL doesn't like my sproc. It says that the sproc has return type none,
Is it possible to accomplish something like this using linqtosql? select * from table

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.