This is a short piece of code but I feel like it could be done more elegantly. What am I missing?The goal: if there are any items in a list, and none of them have a given property, set one of them to have that property.static void GuaranteeAtLeastOne<T>(IEnumerable<T> list, Func<T,bool> getter, Action<T> setter){    if (list.Any() && !list.Any(getter))    {        setter(list.First());    }}
Guarantee at least one in a list
c#;.net;linq
I wouldn't assume to go for the First, rather I'd have a Func<T, bool> predicate parameter which you pass to a .FirstOrDefault(predicate) and ?? it with .First()Also, I'd verify IEnumerable<T> != null before anything.static void GuaranteeAtLeastOne<T>(IEnumerable<T> list, Func<T,bool> getter, Action<T> setter, Func<T,bool> predicate){    if (list == null || list.Count == 0 || list.Any(getter))    {        return;    }    setter(list.FirstOrDefault(predicate) ?? list.First());}