Return a key when a function returns true I've no idea what to call this, so I called it Classify since that's how I use it.

Basically, the idea is to pass a Dictionary&lt;TResult, Func&lt;TValue, bool&gt;&gt; which is evaluated for an input, and the first Func&lt;TValue, bool&gt; returning true will return the TResult key.

The intention is to make this a generic extension method.

public TResult Classify&lt;TValue, TResult&gt;(TValue input, Dictionary&lt;TResult, Func&lt;TValue, bool&gt;&gt; classifications)
{
    foreach (var classification in classifications)
    {
        if (classification.Value.Invoke(input))
        {
            return classification.Key;
        }
    }

    return default(TResult);
}


Pretty simple, but pretty useful (for me at least) as well.

Usage:

&lt;td class='&lt;%#Classify(((ApiBatchLog)Container.DataItem).RequestsPerSecond, new Dictionary&lt;string, Func&lt;double, bool&gt;&gt; { ["good"] = x =&gt; x &gt;= 3, ["neutral"] = x =&gt; x &gt; 2, ["bad"] = x =&gt; true }) %&gt;'&gt;
    &lt;%#((ApiBatchLog)Container.DataItem).RequestsPerSecond.ToString("0.00") %&gt;
&lt;/td&gt;
Needs more LINQ:

return classifications.FirstOrDefault(item =&gt; item.Value.Invoke(input)).Key;


This has the advantage (IMO) of highlighting the fact that you're discarding all but the first match (note that iteration order is not guaranteed!).

Classify doesn't really convey the fact that you're returning a Key from a dictionary. Seems FindKey would be a much better name.

A generic extension method seems like a good idea -  call your source source, and stick to TKey and TValue:

public static TKey FindKey&lt;TKey, TValue&gt;(this IDictionary&lt;TKey, Func&lt;TValue, bool&gt;&gt; source, TValue input)
{
    return source.FirstOrDefault(item =&gt; item.Value.Invoke(input)).Key;
}


That one-liner makes use of the fact that KeyValuePair&lt;TKey,TValue&gt; is a struct (and therefore can't be null), so you can just return .Key and that will be default(TKey).