从'V'到'System.IEquatable <V>'没有装箱转换或类型参数转换

Chr*_*ter 5 c# generics

我有以下扩展方法,它们成功编译并按设计运行.

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();
}
Run Code Online (Sandbox Code Playgroud)

但是,为了避免装箱,我添加了一个新的通用约束,如下所示:

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!
}
Run Code Online (Sandbox Code Playgroud)

我现在收到一个编译错误,其中SelectNotEmpty调用WhereNotEmpty:

类型'V'不能用作泛型类型或方法'MyExtensions.WhereNotEmpty(System.Collections.Generic.IEnumerable)'中的类型参数'T'.从'V'到'System.IEquatable'没有装箱转换或类型参数转换.

我确定我犯了一个愚蠢的错误,但我看不出来.有人能指点一下吗?

Lee*_*Lee 6

你对V的约束应该是

where V : struct, IEquatable<V>
Run Code Online (Sandbox Code Playgroud)

该类型T的WhereNotEmpty应该是IEquatable<T>和你逝去的IEnumerable<V>进入WhereNotEmptySelectNotEmpty应用改造后.

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();
}
Run Code Online (Sandbox Code Playgroud)