There is no box conversion or type parameter conversion from 'V' to 'System.IEquatable <V>'
I have the following extension methods that compile successfully and behave as designed.
public static IEnumerable<T> WhereNotEmpty<T>(this IEnumerable<T> source) where T : struct { return source.Where(item => !item.Equals(default(T))); } public static IEnumerable<V> SelectNotEmpty<T, V>(this IEnumerable<T> source, Func<T, V> selector) where V : struct { return source.Select(selector).WhereNotEmpty(); } However, to avoid boxing, I added a new general restriction as follows:
public static IEnumerable<T> WhereNotEmpty<T>(this IEnumerable<T> source) where T : struct, IEquatable<T> { return source.Where(item => !item.Equals(default(T))); } public static IEnumerable<V> SelectNotEmpty<T, V>(this IEnumerable<T> source, Func<T, V> selector) where V : struct, IEquatable<T> { return source.Select(selector).WhereNotEmpty(); // compile error! } Now I get a compilation error when SelectNotEmpty calls WhereNotEmpty :
Type 'V' cannot be used as a parameter of type 'T' in a generic type or the method 'MyExtensions.WhereNotEmpty (System.Collections.Generic.IEnumerable). There is no box conversion or type parameter conversion from 'V' to 'System.IEquatable'.
I'm sure I made a stupid mistake, but I do not see this. Can someone point me to me please?
Your limit on V should be
where V : struct, IEquatable<V> The type T in WhereNotEmpty must be IEquatable<T> , and you pass IEnumerable<V> to WhereNotEmpty from SelectNotEmpty after applying the transform.
public static IEnumerable<V> SelectNotEmpty<T, V>(this IEnumerable<T> source, Func<T, V> selector) where V : struct, IEquatable<V> { return source.Select(selector).WhereNotEmpty(); } Try changing:
public static IEnumerable<V> SelectNotEmpty<T, V>(this IEnumerable<T> source, Func<T, V> selector) where V : struct, IEquatable<T> { return source.Select(selector).WhereNotEmpty(); } about
public static IEnumerable<V> SelectNotEmpty<T, V>(this IEnumerable<T> source, Func<T, V> selector) where V : struct, IEquatable<V> { return source.Select(selector).WhereNotEmpty(); }