不能在此范围内声明名为“e”的本地或参数

a_f*_*thy 4 .net c# linq

我正在尝试使用此代码:

var controls = new[] { txtName, txtIdentityCard, txtMobile1 };
foreach (var control in controls.Where(e => String.IsNullOrEmpty(e.Text))) // error here in (e)
{
    errorProvider1.SetError(control, "Please fill the required field");
}
Run Code Online (Sandbox Code Playgroud)

检查我的文本框之一是否为空,但出现以下错误:

无法在此范围内声明名为“e”的本地或参数,因为该名称用于封闭本地范围以定义本地或参数

有什么帮助吗?

Sal*_*ari 9

错误明确指出您不能使用,e因为它已经在其他地方使用过,例如在您的事件处理程序或其他地方:

private void Form1_Load(object sender, EventArgs e)//Here for example
Run Code Online (Sandbox Code Playgroud)

尝试其他方法 ( c):

controls.Where(c => String.IsNullOrEmpty(c.Text)))
Run Code Online (Sandbox Code Playgroud)

您还需要errorProvider1.Clear();controls声明下方添加此内容。