c# - Linq - get max of string that ends with an integer -
this question has answer here:
- max() in linq query 2 answers
i'm trying max of string column such - a1 a2 a3 ...
but when max number 10 or above - linq returns a9 max. why?
how solve this?
the code-
from d in db.documents.orderbydescending(d => d.number) (.....) select d.number) .firstordefault();
thanks in advance!
you cant normal linq, if want compare alphanumeric values, should this,
static void main(string[] args) { list<string> vals = new list<string>(); vals.add("a1"); vals.add("a11"); vals.add("a41"); vals.add("a13"); vals.add("a12"); vals.add("a9"); var result = vals.orderbydescending(x => ordernumbers(x)); } public static string ordernumbers(string input) { return regex.replace(input, "[0-9]+", match => match.value.padleft(10, '0')); }
Comments
Post a Comment