For sample class:
private class Tst { public int field1 { get; set; } public string field2 { get; set; } public string field3 { get; set; } }
You can do it as follows:
var index = 1; var sortedList = (from l in list orderby index == 1 ? l.field1.ToString() : index == 2 ? l.field2 : l.field3 select l);
But since the fields have different types, you need to make some type, as you see l.field1.ToString()
How do you do this using lambda:
var sortedList = list.OrderBy(l => index == 1 ? l.field1.ToString() : index == 2 ? l.field2 : l.field3) .Select(l => l).ToList();
source share