0

I need to order my items by a variable from users request. The request gives me a string with field name. There can be over 40 fields so anything like switch / case is not acceptable. I need something like item.OrderBy(x => myString)

Is that possible?

dns_nx
  • 3,651
  • 4
  • 37
  • 66
San
  • 55
  • 7
  • this has already been answered https://stackoverflow.com/questions/2728340/how-can-i-do-an-orderby-with-a-dynamic-string-parameter – Martin Meli Jun 23 '21 at 07:41
  • 1
    Does this answer your question? [How can I do an OrderBy with a dynamic string parameter?](https://stackoverflow.com/questions/2728340/how-can-i-do-an-orderby-with-a-dynamic-string-parameter) – Martin Meli Jun 23 '21 at 07:41
  • Another similar existing: [Dynamic LINQ `OrderBy` on `IEnumerable` / `IQueryable`](https://stackoverflow.com/a/233505/23354) – Marc Gravell Jun 23 '21 at 07:42
  • What kind of sorting of the elements? by val asc? – tartarismo Jun 23 '21 at 07:44
  • But with all of those answers I would need to make a helper class with LINQ to string ex. x=>x.field = string1 and I must avoid this – San Jun 23 '21 at 07:54
  • 1
    @San not sure what you're trying to say there, but I wrote at least one of those answers, and it would work fine as `var ordered = source.OrderBy(myString);`, where `myString` is the member name – Marc Gravell Jun 23 '21 at 08:11

0 Answers0