0

Can I take object property in Linq from string input?

For an example I have collection of clothes:

clothes.OrderBy(x => x.Name);

This will order them by Name, ok. But user should can order them by also Price, Type, Creation Date and so on.

So is it possible to do something like:

clothes.OrderBy(x => x + "." + inputFilter);

Well this is obviously not working like this, but OrderBy doesn't have much methods either.

My idea is to skip some Switch Case construction, if there is some smarter way. Is it such way is my question?

(I`m using asp.net MVC with Razor engine.)

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Jackie
  • 327
  • 2
  • 13
  • another dup: https://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet – Tim Schmelter Oct 26 '17 at 14:36
  • Custom sorts are usually done in a class that inherits IComparable. – jdweng Oct 26 '17 at 14:37
  • Personally It go with with a switch or if statements unless you plan to have a lot of changes made to the properties/columns of that class/table. – juharr Oct 26 '17 at 14:37
  • Use dynamic Linq - https://stackoverflow.com/questions/15388230/linq-to-sql-sort-query-by-arbitrary-propertycolumn-name – David P Oct 26 '17 at 14:38

0 Answers0