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
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).