Vashomaru
How to sort a list of a class by property using LINQ when the name of the property comes from input
So, im learning c# and im trying to solve an exercise.
The issue im facing is, i have a class that has 3 int properties, for example population, surface and diameter, and i want to get from a list of that class, the objects with the max and the min of one of those properties chosen from an input on the console. So, if i write in the console "population", filter the list and retrieve the ones with the most and least population. I thought about using LINQ OrderBy and then pick the first and last items, but im blanking out on how to use the input i get to do that, since i get a string like "population" from the console and i can't use it in OrderBy(x => x.population). I feel like its probably really stupid im forgetting but my head is so burned right now that i can't come up with anything
10 replies