I seem to often (when dealing with enums) do things like this:
.Select(x => new {
enumDesc = (
x.status == GoodStatus ? "Good!"
: x.status == BadStatus ? "Bad<angryface>"
: x.status == Unknown ? "no clue"
)
})
or
let enumDesc = (
x.status == GoodStatus ? "Good!"
: x.status == BadStatus ? "Bad<angryface>"
: x.status == Unknown ? "no clue"
)
Some queries end up having dozens of these conditions, like a project I'm working on now. Doing it this way is nice because it can be translated into a SQL case statement so it's faster than iterating through after materialization. In my current project, however, I need to do this in more than one place/query. Is there any way to do this in a performant and reusable way (i.e., translates to SQL) without refactoring the structures (e.g. moving the enum into a table)? I haven't been able to come up with one. If I could capture or alias just the ternary stuff, that would be good enough, or making an expression that "selects" from constants or attributes or something...