1

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
15
  • 1
    If you were writing real SQL, this would be easy: ORDER BY CONVERT(INT, SUBSTRING(Cust_ID, 2, 255)) ... sorry but I don't know how to translate this into whatever you're using (which I presume is supposed to make your database access easier, but it appears that is not always the case). Aug 24, 2012 at 0:48
  • If it was real SQL, the ORDER BY wouldn't cause it to not select anything... Aug 24, 2012 at 0:57
  • by the way, is the leading 'C' character always uppercase
    – staafl
    Aug 24, 2012 at 1:07
  • I really don't believe the order by itself is causing that. Perhaps it's how this specific framework is trying (unsuccessfully) to implement the order by. Aug 24, 2012 at 1:07
  • @Aaron Bertrand It's like you have reservation for this framework :)
    – codingbiz
    Aug 24, 2012 at 1:11

5 Answers 5

2

You can just use AsEnumerable to switch to LINQ to Objects before OrderBy. In other words, you'd be executing OrderBy client-side instead of letting the ORM attempt (unsuccessfully, as it were) map it to SQL's ORDER BY.

That being said, it's a bad idea to store in the database a prefix that never changes. It wastes space, forces you to use the wrong data type (VARCHAR instead of INT), triggers a wrong kind of comparison in ORDER BY and prevents ORDER BY from using an index (although your WHERE seems very effective at doing that already)...

6
  • I have always used ToArray as I never knew about AsEnumerable. I was thinking it would be nice to have a way to do this without having to store all results in an array. With regards your comment about not storing the C, I would say that you should store whatever value is used in real life. If that piece of data is referred to as "C123" then you should store C123. Storing it as an int would be a mistake imo.
    – MikeKulls
    Aug 24, 2012 at 3:38
  • @MikeKulls "store whatever value is used in real life" And 'C' is not used. It's just displayed. Aug 24, 2012 at 3:57
  • @BrankoDimitrijevic I would say displaying it is using it. Let's say the C123 is a customer code and this is commonly printed on customer correspondance and internal documents then I think the C is being used. If we stored just the 123 as an int then this could cause problems if they start having customer codes with leading zeros or if someone comes up with idea of prefixing international customers with an I. It could also cause problems interfacing with other systems that store it as C123.
    – MikeKulls
    Aug 24, 2012 at 4:36
  • @MikeKulls Then prefix is not always the same and the whole line of reasoning changes. I was commenting only on the case when prefix never changes. Aug 24, 2012 at 5:03
  • @MikeKulls BTW, if the prefix does change, you might still want to separate string prefix and int suffix fields, for storage efficiency and indexing ability (an index on int, not string, is required for efficiently executing ORDER BY when integer comparison is desired). After all, it's easy to create a calculated field or a view that concatenates them together. You could go the opposite route (have a single physical string field, then split it to 2 calculated fields and the index them), but this could be less efficient storage-wise. Aug 24, 2012 at 5:04
1

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.

4
  • great! that works - after I edited out minor typos: .OrderBy(u => Int32.Parse(u.Cust_ID.Substring(1)))
    – Josh
    Aug 24, 2012 at 2:07
  • Yes, actually I don't need to modify anything here. How would I rewrite it as an array? I have found the use case you demonstrate that makes my concatenate a bit strange, so I've edited to the following which works as expected .Where(u => (u.Cust_ID + " " + u.Given_Name + " " + u.Surname).Contains(searchstring))
    – Josh
    Aug 24, 2012 at 2:10
  • @Josh oops, I will fix. To return an array just use ToArray instead of ToList and define customerList as "Customer[]"
    – MikeKulls
    Aug 24, 2012 at 2:24
  • "My apologies if it's unethical to take parts from other answers." It's perfectly fine, as long as you give proper credit, which you did! Aug 24, 2012 at 4:03
0

my guess is your database contains values that aren't of the form 'C[0-9]+' and are incompatible with int.Parse, possibly 'null', causing your OrderBy to raise an exception

5
  • try checking db.Customers.Any(c => c.Cust_ID == null) and db.Customers.Any(c => !Regex.IsMatch(c.Cust_ID, "^C[0-9]+$")) to verify... or indeed, db.Customers.Any(c => c == null)
    – staafl
    Aug 24, 2012 at 1:01
  • the regex won't compile (The name 'Regex' doesn't exist in the current context). everything seems to run without throwing up an exception.
    – Josh
    Aug 24, 2012 at 1:18
  • Hmm, I'm not sure if I'm doing this right, I tried using an if statement to throw up an alert on a found null, but this doesn't seem to return a null even though I know there is one in the table in a different row: if (db.Customers.Any(c => c == null))
    – Josh
    Aug 24, 2012 at 1:23
  • Wait, I have an issue with my alert implementation, let me fix that and get back to you
    – Josh
    Aug 24, 2012 at 1:25
  • ok, no, doesn't throw out a null, but now I can see an exception on the query which I didn't see before. Have added it to original question.
    – Josh
    Aug 24, 2012 at 1:29
0

I don't know much about LINQ but try this

.OrderBy(u => Convert.ToInt32(u.Cust_ID.Substring(1)))
3
  • That still results in id C10 coming before C2
    – Josh
    Aug 24, 2012 at 1:01
  • I have updated the answer. This will only work if the rest of string after C are numbers
    – codingbiz
    Aug 24, 2012 at 1:04
  • shouldn't make a difference to what the op tried. Convert.ToInt32 and int.Parse are equivalent in the usual case (string of decimal digits). do check my answer - i'm pretty sure something is amiss with your dataset
    – staafl
    Aug 24, 2012 at 1:06
0

I agree with MikeKulls that sorting could be done after pulling the data, but if you do prefer sorting on the server, you could pad the strings to get the semantics of integer sorting:

customerlist = db.Customers
                .Where(u => (u.Cust_ID+u.Given_Name+u.Surname).Contains(searchstring))
                .OrderBy(u => u.Cust_ID.Substring(1).PadLeft(10 /* or suitable number */, '0'))
                .ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.