简体   繁体   English

如何将Lambda表达式用作字符串参数C#

[英]How to use Lambda Expression as a String parameter C#

Just for curiosity, I would like to know if it is possible to use a Lambda Expression as a String parameter? 出于好奇,我想知道是否可以使用Lambda表达式作为String参数?

Here's my snipet: 这是我的摘录:

List<Int64> selectedUsers = GetSelectedUsers();
if (MessageBox.Show(this, ((String message) =>
{
    message = "Are you sure you want to delete user(s) ID";
    foreach (Int64 id in selectedUsers)
    {
        message += " " + id.ToString();
    }
    message += "?";
    return message;
}), "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{ 
    //DoSomething();
}

But unfortunately, I get this error "Error 7 Cannot convert lambda expression to type 'string' because it is not a delegate type" here: (String message) 但是,不幸的是,我在这里收到此错误“错误7无法将lambda表达式转换为类型'string',因为它不是委托类型” :(字符串消息)

Thanks for your help, it is really appreciated! 感谢您的帮助,我们非常感谢!

Build your message before 建立您的信息之前

string ids = selectedUsers.Select(n=>n.ToString()).Aggregate((current, next) => current + ", " + next);
// also works string.Join
// string ids = string.Join(", ",selectedUsers);
string message = "Are you sure you want to delete user(s) ID: "+ids+"?";
if (MessageBox.Show(this, message, "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{ 
    //DoSomething();
}

If you really wanted to use a delegate of some sort to return a string (and I'm honestly not sure why you would here), you'd need to cast the expression to Func<string> and immediately invoke it. 如果您真的想使用某种委托来返回字符串(老实说,我不确定您为什么会在这里),则需要将表达式转换为Func<string>并立即调用它。

if (MessageBox.Show(this, 
    ((Func<string>)(() =>
        {
            var message = "Are you sure you want to delete user(s) ID";
            foreach (Int64 id in selectedUsers)
            {
                message += " " + id.ToString();
            }
            message += "?";
            return message;
       })
    )(), 
    "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    { 
        //DoSomething();
    }
}

I think this is what you should do 我认为这是你应该做的

List<Int64> selectedUsers = GetSelectedUsers();

            if (MessageBox.Show(this, ((Func<string, string>)(message =>
            {
                message = "Are you sure you want to delete user(s) ID";
                foreach (Int64 id in selectedUsers)
                {
                    message += " " + id.ToString();
                }
                message += "?";
                return message;
            }))(""), "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                //DoSomething();
            }
        }

what we have here is an IFFE. 我们这里有一个IFFE。 As an optimization you could get rid of the message argument, since you're not really using the initial value. 作为一种优化,您可以摆脱message参数,因为您并没有真正使用初始值。 This would become : 这将变成:

List<Int64> selectedUsers = GetSelectedUsers();

            if (MessageBox.Show(this, ((Func<string>)(() =>
            {
                var message = "Are you sure you want to delete user(s) ID";
                foreach (Int64 id in selectedUsers)
                {
                    message += " " + id.ToString();
                }
                message += "?";
                return message;
            }))(), "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                //DoSomething();
            }

I hope this helps 我希望这有帮助

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

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