I have a class with the following structure
public class DisplayItem
{
public string Id { get; set; }
public SortFields SortFields { get; set; }
}
with a sub class
public class SortFields
{
public string ProductNumber { get; set; }
public DateTime DateOfUpload { get; set; }
}
So the idea is to sort a List based on the values in the SortFields class properties.
The most basic solution i can think of is to do something like this
public IEnumerable<DisplayItem> Sort(IEnumerable<DisplayItem> source, string sortBy, SortDirection sortDirection)
{
switch (sortBy)
{
case "Product Number":
switch (sortDirection)
{
case SortDirection.Ascending:
return source.OrderBy(x => x.SortFields.ProductNumber);
case SortDirection.Descending:
return source.OrderByDescending(x => x.SortFields.ProductNumber);
}
case "Uploaded Date":
{
//--do--
}
}
}
Ideally i would like to be able to pass the sortBy as a parameter in the orderby method and while i did find a couple of solutions on the internet , they dont seem to be able to sort the list of the base class based on subclass property.
is there a way that we can pass the sub class property as a parameter and be able to sort the list the parent class?