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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:45:22+00:00 2026-05-17T06:45:22+00:00

I have a query in linqtosql that returns a LabelNumber: var q = from

  • 0

I have a query in linqtosql that returns a LabelNumber:

var q = from list in db.Lists
        select list.LabelNumber;

var q then becomes an IEnumerable<string> with elements like this:

{"1","2","2.A","2.B","3","3.A","3.B"}

I basically want to order the elements as they appear above, but I can’t use the OrderBy(x=>x.LabelNumber) because "10" would get placed after "1" and before "2".

I assume I have to write a custom comparator function, but how do I do this with linq?

Edit: I think all of the answers below will work, but one caveat must be added to all responses.

If you are using Linq2SQL you cannot use array indexes within the query. To overcome this, you should have two queries. One that reads from SQL. The second does the ordering:

var q = from list in db.Lists
            select list.LabelNumber;

var q2 = q.AsEnumerable()
          .OrderBy(x => int.Parse(x.LabelNumber.Split('.')[0]))
          .ThenBy(x => x.Number
                        .Contains(".") ? 
                              x.LabelNumber.Split('.')[1].ToString() 
                              : 
                              string.Empty);
  • 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-17T06:45:23+00:00Added an answer on May 17, 2026 at 6:45 am

    You probably don’t have to write a custom comparer. If all your labels are in the form number.letter, you could use this.

    var query = from list in db.Lists
                let split = list.LabelNumber.Split('.')
                let order = split.Length == 1
                    ? new { a = int.Parse(split[0]), b = String.Empty }
                    : new { a = int.Parse(split[0]), b = split[1] }
                orderby order.a, order.b
                select list.LabelNumber;
    

    If you need more control, you could always convert the sortby fields (a and b) to the appropriate types rather than ints and strings.


    If this is LINQ-to-SQL, this actually won’t work since some methods used here are not supported. Here’s a LINQ-to-SQL friendly version. It won’t yield the prettiest query, but it will work.

    var query = from list in db.Lists
                let dot = list.LabelNumber.IndexOf('.')
                let name = list.LabelNumber
                let order = dot == -1
                    ? new { a = Convert.ToInt32(name.Substring(0, dot)), b = String.Empty }
                    : new { a = Convert.ToInt32(name.Substring(0, dot)), b = name.Substring(dot+1) }
                orderby order.a, order.b
                select list.LabelNumber;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a LinqtoSql query that I now want to precompile. var unorderedc = from
I have a Linq2Sql query that looks like this: var data = from d
Ok, I have a LinqToXml query that returns a single record (correct terminology?), however,
I have query like this : SELECT EXTRACT(MONTH FROM d.mydate) AS synmonth, SUM(apcp) AS
I have a query that successfully grabs the unique products from my products table
I have a selection list that is generated dynamically, it lists a number of
I have a query that returns IQueryable, my query consists of join. the end
I have query: select id, name from categories where parent_id in ( select id
I'm stuck on translating a left outer join from LINQToSQL that returns unique parent
I am using in C# MYsql .I have query that works if I run

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.