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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:48:49+00:00 2026-06-03T20:48:49+00:00

I have a method in a webservice that has parameter with which users can

  • 0

I have a method in a webservice that has parameter with which users can decide how they want to order their results. This is a List(Of String) with the names of the fields in the order they want to sort them.

I know I can normally order on multiple columns by doing the following

Dim test = Bars.OrderBy(Function(x) x.Foo) _
               .ThenBy(Function(x) x.Bar) _
               .ThenBy(Function(x) x.Test)

However in this case this won’t work since I can’t chain the ThenBy function because I’m adding the sorting orders in a loop. To use ThenBy I need an IOrderedQueryable collection. This is how I would want it to work

Dim sortColumns = {"Foo", "Bar", "Test"}
Dim query = From b in Bars
For each column in sortColumns
    Select Case column
        Case "Foo"
            query = query.Orderby(Function(x) x.Foo)
        Case "Bar"
            query = query.Orderby(Function(x) x.Bar)
        Case "Test"
            query = query.Orderby(Function(x) x.Test)
    End Select
Next

Dim result = query.Select(Function(x) x.x).ToList()
Return result

This of course won’t work because OrderBy will replace any previous ordering. The only solution I can think of is ordering the list on some other variable first so I already have an IOrderedQueryable collection but this just seems like the wrong approach.

Dim bars As New List(Of Bar)
Dim sortColumns = {"Foo", "Bar", "Test"}
Dim query = bars.Select(Function(x) New With {.Temp = 1, .x = x}) _
                .OrderBy(Function(x) x.Temp)

For Each column In sortColumns
    Select Case column
        Case "Foo"
            query = query.ThenBy(Function(x) x.x.Foo)
        Case "Bar"
            query = query.ThenBy(Function(x) x.x.Bar)
        Case "Test"
            query = query.ThenBy(Function(x) x.x.Test)
    End Select
Next

Dim result = query.Select(Function(x) x.x).ToList()
Return result    
  • 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-03T20:48:50+00:00Added an answer on June 3, 2026 at 8:48 pm

    You could write your own extension method OrderByOrThenBy which checks whether the value is already an IOrderedQueryable, uses ThenBy if so and OrderBy otherwise. Slightly smelly, but not terribly hard to do.

    EDIT: C# sample (untested):

    public static class QueryableOrdering
    {
        public static IOrderedQueryable<TElement> OrderByOrThenBy<TElement, TKey>
            (this IQueryable<TElement> source,
             Expression<Func<TElement, TKey>> ordering)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (ordering == null)
            {
                throw new ArgumentNullException("ordering");
            }
            var ordered = source as IOrderedQueryable<TElement>;
            return ordered == null ? source.OrderBy(ordering)
                                   : ordered.ThenBy(ordering);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a winforms, and it connecting wit webservice. Webservice has method which create
I have a webservice method that reads a photo and returns its byte data.
I have a webservice which returns the content of a pdf. I've noticed that
I have a web service with a method that has two string parameters. When
I have a webservice method getContactsAsync. I've understood that when it's completed it's supposed
I have a Web Service with Just one Web Method that returns a boolean
I have implemented a ToString() override method for my class in my Webservice and
I have to map a REST Webservice URL like http://server:8080/application/service/customer/v1 to createCustomer method in
I have a method in my web service which returns a DataView, I have
I have a method which requests external webservices and returns json here: public string

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.