I have a query in linqtosql that returns LabelNumber:
var q = from list in db.Lists select list.LabelNumber;
var q then becomes an IEnumerable<string> with the following elements:
{"1","2","2.A","2.B","3","3.A","3.B"}
Basically I want to arrange the elements as they appear above, but I cannot use OrderBy(x=>x.LabelNumber) , because "10" will be placed after "1" and before "2" .
I suppose I need to write a custom comparator function, but how to do it with linq?
Edit: I think all of the answers below will work, but for all the answers you need to add one warning.
If you use Linq2SQL, you cannot use array indices in a query. To overcome this, you must have two requests. One that reads from SQL. Second order:
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);
c # linq
Shawn
source share