Here is the full implementation I am considering:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace Utils {
public static class IDictionaryExt {
public static IEnumerable<SelectListItem> ToSelectListItems<T, R>(this IDictionary<T, R> dic, T selectedKey) {
return dic.Select(x => new SelectListItem() { Text = x.Value.ToString(), Value = x.Key.ToString(), Selected=(dynamic)x.Key == (dynamic)selectedKey });
}
}
}
Notice the equality check using the dynamic casts: (dynamic)x.Key == (dynamic)selectedKey
. Is this the best way to check equality between selectedKey
and x.Key
here? Based on @Gabe's comment in Operator '==' can't be applied to type T?, I believe it is: overload resolution is deferred to to runtime, but we do get "normal" overload resolution (i.e. considering ValueType
s and other Object
s with ==
overloads versus Object
s with default reference equality).