So, a fairly common extension method for IEnumerable, Run:
public static IEnumerable<T> Run<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var item in source)
{
action(item);
yield return item;
}
}
When I try to use that with, for instance, DbSet.Add:
invoice.Items.Run(db.InvoiceItems.Add);
// NB: Add method signature is
// public T Add(T item) { ... }
... the compiler complains that it has the wrong return type, because it is expecting a void method. So, add an overload for Run that takes a Func instead of Action:
public static IEnumerable<T> Run<T>(this IEnumerable<T> source, Func<T, T> action)
{
return source.Select(action).ToList().AsEnumerable();
}
And now the compiler complains that "The call is ambiguous between the following methods..."
So my question is, how can the Action overload of the Run method cause ambiguity when it is not valid for the method group?