小编use*_*286的帖子

C#将System.Func <Tderived,bool>转换为System/Func <Tbase,bool>

我用谷歌搜索,但找不到满意的答案.我基本上试图让这段代码工作:

public List<WordEntry> WordDataBase = new List<WordEntry>();
public List<CharacterEntry> CharacterDataBase = new List<CharacterEntry>();

public List<Entry> SelectWhere<T>(System.Func<T, bool> predicate) where T : Entry
{
    if (typeof(T) == typeof(WordEntry))
        return WordDataBase.Where(predicate);
    else if (typeof(T) == typeof(CharacterEntry))
        return CharacterDataBase.Where(predicate);
    else
        return null;
} 
Run Code Online (Sandbox Code Playgroud)

在此示例中,WordEntry和CharacterEntry都是从Entry派生的.我得到编译器错误:

Error   CS1503  Argument 2: cannot convert from 'System.Func<T, bool>' to 'System.Func<WordEntry, int, bool>'   
Run Code Online (Sandbox Code Playgroud)

Error   CS1503  Argument 2: cannot convert from 'System.Func<T, bool>' to 'System.Func<CharacterEntry, int, bool>'
Run Code Online (Sandbox Code Playgroud)

希望你能帮助我.提前致谢

c# linq derived func

1
推荐指数
1
解决办法
1547
查看次数

标签 统计

c# ×1

derived ×1

func ×1

linq ×1