c# - 不能在此范围内声明名为 'e' 的局部变量或参数

标签 c# .net 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");
}

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

A local or parameter named 'e' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

有什么帮助吗?

最佳答案

错误清楚地表明你不能使用 e 因为它已经在其他地方使用过,比如在你的事件处理程序或其他地方:

private void Form1_Load(object sender, EventArgs e)//Here for example

试试别的(c):

controls.Where(c => String.IsNullOrEmpty(c.Text)))

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

关于c# - 不能在此范围内声明名为 'e' 的局部变量或参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246472/

相关文章:

c# - C#中如何判断一个线程是否繁忙?

C# 如何将 system.windows.media.brush 转换为 system.drawing.brush

c# - 断言在库初始化期间未被 except block 捕获

c# - 如何使用 Linq Skip 和 Take 调用 Expression<Func<T, bool>> 参数化函数

C# - 实时更新图表

c# - 通过对静态类的反射设置属性

.net - Json.NET 以最小的小数位数序列化 float/double,即没有冗余 ".0"?

c# - Entity Framework - 大写第一个属性名称字母

c# - C# 中的 Jaxb 等价物

c# - Linq,使用 ToLookup 将值投影到不同的命名变量