How to replace switch statement of expressions

Viewed 609

I have some code switching based on the enum value passed to it which then performs a database query (via EF)

switch(regtype)
{
    case RegType.Type1:
        return (Dc.ListPeople(n => n.RegistrationType1Id != null)
        .DefaultIfEmpty()
        .Max(n => n.RegistrationType1Id ) ?? 0) + 1;
    case RegType.Type2:
        return (Dc.ListPeople(n => n.RegistrationType2Id != null)
        .DefaultIfEmpty()
        .Max(n => n.RegistrationType2Id ) ?? 0) + 1;
...
}

Now the data model is what it is, let's look past that. RegistrationType_N_Id is an int?, ListPeople takes an argument of Expression<Func<Person, bool>>.

There are a total of 3 enum values, so it's not that bad, but even just for the mental exercise, I'd like to know if I could replace this switch statement with something more fancy.

I thought of a Dictionary<RegType, Expression<Func<something>>>, but since the first use in the db predicate is different from the 2nd in Max() got me stumped.

1 Answers
Related