简体   繁体   English

这个lambda表达式有什么问题?

[英]What is wrong with this lambda expression?

I am really struggling to understand why, when I change my code to use a lamdba expression, it doesn't work. 我真的很难理解为什么,当我更改代码以使用lamdba表达式时,它不起作用。

This code works and prints on console: 此代码可以在控制台上运行并打印:

object dummy = new object();
InterServer.ExecuteDataReader(new InterServerRequest(ServerID.a_01, "dbo.getbooks") 
    { 
        Params = new Dictionary<string, object> { 
            { "Tool", "d1" }, 
            { "Loc", locale == string.Empty ? null : locale } } 
    },
    (_, reader) =>
        {
            reader.AsEnumerable(r => (r.GetString(r.GetOrdinal("book")))).ToList().ForEach(Console.WriteLine);
            return new Response(dummy);
        }
    );

This code has been changed to use a lambda expression; 该代码已更改为使用lambda表达式; it doesn't print anything, I don't understand why: 它什么也没打印,我不明白为什么:

InterServer.ExecuteDataReader(new InterServerRequest(ServerID.a_01, "dbo.getbooks")
    { 
        Params = new Dictionary<string, object> { 
            { "Tool", "d1" }, 
            { "Loc", locale == string.Empty ? null : locale } } 
    },
    (_, reader) =>
        {
            return new Response(new Action(() => 
                reader.AsEnumerable(r =>(r.GetString(r.GetOrdinal("book")))).ToList().ForEach(Console.WriteLine)));
        }
    );

This is really driving me mad as I don't see if I am doing anything wrong. 这真让我发疯,因为我看不出自己做错了什么。 Can someone help? 有人可以帮忙吗?

Thanks, AG 谢谢,AG

What are you trying to achieve by making it use a lambda expression? 您想通过使用Lambda表达式来实现什么?

In this example, the Action is not invoked, therefore nothing happens. 在这个例子中, Action不会被调用,因此没有任何反应。 When the Action will be invoked you'll see the output. Action被调用时,您将看到输出。

The Action is not executed implicitly. Action不是隐式执行的。

For example: 例如:

public void Call()
{
    // Call with int argument 1
    DoSomething(1);

    // Call with Func that returns 1
    // It'll never produce the value unless Func is invoked
    DoSomething(new Func<int>(() => 1));

    // Call with Func explicitly invoked
    // In this case the body of the lambda will be executed
    DoSomething(new Func<int>(() => 1).Invoke());
}

public void DoSomething(object obj)
{

}

Calling the Response constructor with Action will actually send the Action . 使用Action调用Response构造函数实际上将发送Action If Response has an overload that invokes it and uses the return value, then the Action will appear only when the Response actually uses it (not necessary during the constructor call). 如果Response有一个调用它并使用返回值的重载,则该Action仅在Response实际使用它时才会出现(在构造函数调用中不必要)。

In your example, the work will be done anyway during the call to (_, reader) => ... so there is no work done "too early". 在您的示例中,无论如何在调用(_, reader) => ...过程中都会完成工作(_, reader) => ...因此不会“太早”完成工作。

To make it clear (last sample I add :) ): 为了清楚起见(最后一个示例,我添加了:)):

public void Call()
{
    DoSomething(new Action(() => Console.WriteLine("Arrived")));
}

public void DoSomething(Action obj)
{
    int x = 0; // Nothing printed yet
    Action storedAction = obj; // Nothing printed yet
    storedAction.Invoke(); // Message will be printed
}

Unless Response expects an Action it'll never get to execute its body. 除非Response期望有一个Action ,否则它将永远无法执行其主体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM