Let's say we have some types defined like this:
public class BaseClass { public int Value { get; set; } } public class SubClassA : BaseClass { public bool SomeBoolValue { get; set; } } public class SubClassB : BaseClass { public decimal SomeDecimalValue { get; set; } }
And we will build two type subclass type lists
List<SubClassA> subClassAList = new List<SubClassA> { new SubClassA {SomeBoolValue = true, Value = 0}, new SubClassA {SomeBoolValue = true, Value = 2}, new SubClassA {SomeBoolValue = false, Value = 1}, }; List<SubClassB> subClassBList = new List<SubClassB> { new SubClassB {SomeDecimalValue = 1.3M, Value = 2}, new SubClassB {SomeDecimalValue = 3.5M, Value = 1}, new SubClassB {SomeDecimalValue = 0.2M, Value = 5}, };
Is there a way to implement a function that can filter both subClassAList and subClassBList in the Value property? I know that this can be achieved by casting the results of such a function:
public static IEnumerable<BaseClass> OrderList(IEnumerable<BaseClass> list) { return list.OrderBy(x => x.Value); } ... ... orderedAList = OrderList(subClassAList) .Cast<SubClassA>()
But then we need to pass the results in order to return them back to the list of the subclass, which does not look very typical. I know that the ordering operation itself is very simple and probably does not require a separate function to perform the action, but if you are trying to do something more complex than ordering, it would be useful to contain the logic for a single function / class / what, to achieve this, not copy the code.
source share