C# re-use LINQ expression for different properties with same type

Viewed 868

I have a class with several int properties:

class Foo
{
    string bar {get; set;}
    int a {get; set;}
    int b {get; set;}    
    int c {get; set;}
}

I have a LINQ expression I wish to use on a List<Foo>. I want to be able to use this expression to filter/select from the list by looking at any of the three properties. For example, if I were filtering by a:

return listOfFoo.Where(f => f.a >= 0).OrderBy(f => f.a).Take(5).Select(f => f.bar);

However, I want to be able to do that with any of f.a, f.b, or f.c. Rather than re-type the LINQ expression 3 times, I'd like to have some method which would take an argument to specify which of a, b, or c I want to filter on, and then return that result.

Is there any way to do this in C#? Nothing immediately comes to mind, but it feels like something that should be possible.

1 Answers
Related