I have an IEnumerable that I’ve materialized from Linq2Sql. I’ve already filtered out the records I want, now I want to order them based on a selected enum:
public enum Sort
{
Time,
Name,
Value
}
public class LinqClass
{
public DateTime Time;
public string Name;
public double Value;
}
Sort sort = Sort.Time
items.OrderBy(sort);
What is the best way to do this? I could create an overloaded OrderBy(Sort s) that is just a big switch statement:
switch(sort)
case Time:
return this.OrderBy(x=>x.Time);
I could probably do something with a dictionary too. Any other ideas, or is there a standard pattern for doing this.
A switch statement is probably the best approach, as it makes it easy to spot when an invalid value has been passed.
You could use a
Dictionary<Sort, Func<IEnumerable<LinqClass>, IEnumerable<LinqClass>>>but I don’t think it would be worth it.