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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:12:27+00:00 2026-06-10T05:12:27+00:00

How do I order a list by the value of a string minus it’s

  • 0

How do I order a list by the value of a string minus it’s first character (in my case the first character is always a C followed by a number), while performing a database search.

I’ve tried a lot of stuff but I’m pretty much stumbling around in the dark, I’m sure someone here can solve this in a instant.

My code that works (but doesn’t do what I want):

        List<Customer> customerlist = null;
        try
        {
            customerlist = db.Customers
                            .Where(u => (u.Cust_ID+u.Given_Name+u.Surname).Contains(searchstring))
                            .OrderBy(u => u.Cust_ID)
                            .ToList();
        }

My buggy code at the moment (which seems to me like it should work, but results in the search always returning no results):

        List<Customer> customerlist = null;
        try
        {
            customerlist = db.Customers
                            .Where(u => (u.Cust_ID+u.Given_Name+u.Surname).Contains(searchstring))
                            .OrderBy(u => Int32.Parse(u.Cust_ID.TrimStart('C')))
                            .ToList();
        }

More details:

I have a column in my table named Cust_ID, which starts with a C and is then followed by a number without leading zeros e.g. C1, C2, … C43, … C999.

I am allowing the user to perform a search, the query of which is assigned to searchstring.

I’ve tried debugging by removing the int parse

                            .OrderBy(u => u.Cust_ID.TrimStart('C'))

that returns no results as well.

The original code I had in here still works to complete the search, but doesn’t sort it correctly.

                            .OrderBy(u => u.Cust_ID)

I tried using Replace instead of TrimStart

                            .OrderBy(u => u.Cust_ID.Remove(0, 1))

And this returns a result, but still doesn’t sort, and when i put the Int parse in, it stops returning a result again:

                            .OrderBy(u => Int32.Parse(u.Cust_ID.Remove(0, 1)))

I tried switching the int parse to:

                            .OrderBy(u => int.Parse(u.Cust_ID.Remove(0, 1)))

but that didn’t help.

edit:
found I was getting an exception:

System.NotSupportedException: LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)
       at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
       at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)
       at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
       at System.Data.Objects.ELinq.ExpressionConverter.Convert()
       at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
       at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
       at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at lab2.Controllers.DemoController.retrievecustomers() in [[Directory]]Controller.cs:line 52
  • 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-10T05:12:29+00:00Added an answer on June 10, 2026 at 5:12 am

    I would just do the ordering on the client side. Why? Because it doesn’t increase the amount of traffic going across the network and will give you a lot more flexibility and allow you to check for dodgy values. It could be argued that it is better to do the sorting on the client anyway as it reduces server load.

    customerlist = db.Customers
        .Where(u => (u.Cust_ID+u.Given_Name+u.Surname).Contains(searchstring))
        .AsEnumerable()  //EDIT: was ToArray()
        .OrderBy(u => Int32.Parse(u.Cust_ID.SubString(1)))
        .ToList();
    

    Note that the AsEnumerable causes the linq query up to that point to be sent to the server and everything afterwards is done on the client.

    As a side note I would add that if you don’t need to modify the list then it would be slightly more efficient to store the values in an array instead of a list.

    I would also add do you really want to join CustID with their names for the search? This would cause the search term Bob to match someone called Bo Brown.

    EDIT: I have changed ToArray to AsEnumerable as this is more efficient as pointed out by Branko Dimitrijevic. My apologies if it’s unethical to take parts from other answers.

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

Sidebar

Related Questions

How do I order by a passed string value on my list of objects?
I need to sort list of strings in the alphabetical order: List<String> list =
I have a simple linked list. The node contains a string (value) and an
I need to prepend a single value to an IEnumerable (in this case, IEnumerable<string[]>
I have a number of enums and need to get them as List<string> objects
I have this controller method: [GET(/whatever/list)] public ActionResult Index(string sortby, string order) I'm trying
I'm trying to sort a list of CLLocationDistance values by closest distance (ascending order).
I have a order list and I want to generate and rank the product
I have to order a list of child entities by dates. Right now I
I need a jQuery to order a list like this: http://jqueryui.com/demos/sortable/ But I need

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.