C# c:枚举类中的Where()、OrderBy()和Select()不应该将委托类型、lambda表达式或匿名类型作为参数吗

C# c:枚举类中的Where()、OrderBy()和Select()不应该将委托类型、lambda表达式或匿名类型作为参数吗,c#,linq,lambda,anonymous,enumerable,C#,Linq,Lambda,Anonymous,Enumerable,我有一个问题。请参阅两段代码。不应该将委托类型、lambda表达式或匿名类型作为参数。如果是,QueryOverStringWithRawDelegate如何产生与QueryOverstringWithExtensionMethods相同的结果 谢谢你的帮助 不,正如您所观察到的,这不是一个错误。编译器会将具有适当签名的方法组转换为相应的Func类型 简单地说就是: currentVideoGames.Where(new Func<string, bool>(Filter)) cu

我有一个问题。请参阅两段代码。不应该将委托类型、lambda表达式或匿名类型作为参数。如果是,QueryOverStringWithRawDelegate如何产生与QueryOverstringWithExtensionMethods相同的结果


谢谢你的帮助

不,正如您所观察到的,这不是一个错误。编译器会将具有适当签名的方法组转换为相应的Func类型

简单地说就是:

currentVideoGames.Where(new Func<string, bool>(Filter))
currentVideoGames.Where(new Func<string, bool>(CompilerGeneratedFunction))
而不是:

SomeEvent += new EventHandler(Handler);

同样地:

currentVideoGames.Where(game => game.Contains(" "))
是以下的简写:

currentVideoGames.Where(new Func<string, bool>(Filter))
currentVideoGames.Where(new Func<string, bool>(CompilerGeneratedFunction))

。碰巧编译器将其称为b__0_0的CompilerGeneratedFunction放入一个新的内部类中,并且出于性能原因,它缓存了实例化的Func。

如果我是你,我会将结果包括在你的问题中,并解释你预期会发生的情况。输出:Uncharted 2,Fallout 3,Bio Shock 4对于第二种方法的两种方法,我预计会出现异常。作为一个Func的参数,谢谢你的详细解释!
currentVideoGames.Where(game => game.Contains(" "))
currentVideoGames.Where(new Func<string, bool>(CompilerGeneratedFunction))
bool CompilerGeneratedFunction(string x)
{
    return x.Contains(" ");
}