Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“int”_C#_Linq - Fatal编程技术网

C# LINQ:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“int”

C# LINQ:无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“int”,c#,linq,C#,Linq,我从以下代码中得到一个错误: int dreamX[]; private void Form1_Load(object sender, EventArgs e) { sumX(); } private void sumX() { for (var i = 0; i < 8; i++) { dreamX[i] = from Control control in Controls where

我从以下代码中得到一个错误:

int dreamX[];

private void Form1_Load(object sender, EventArgs e)
{ 
    sumX();
}

private void sumX()
{
    for (var i = 0; i < 8; i++)
    {
        dreamX[i] =
            from Control control in Controls
            where
                control.GetType() == typeof(TextBox)
                && control.Name.StartsWith("box")
            select Convert.ToInt32(((TextBox)control).Text);
    }
}
我得到这个错误,如何显式转换它

无法将类型“System.Collections.Generic.IEnumerable-int-”隐式转换为“int”

FirstOrDefault将IEnumerable转换为int


实际上,它首先出现在linq查询的outputresult中。

好的,该查询可能返回多个值,因此您需要使用、或扩展方法

请注意,只有当列表中只有一个元素时,单元素才起作用;只有当列表中至少有一个元素时,第一元素才起作用。如果列表中没有元素,FirstOrDefault将恢复为默认值0


根据您的具体需要,您将不得不选择:

这有很多问题

首先,您尝试将可能有许多转换的整数分配给数组中的单个整数。这就是错误消息告诉您的

此外,在您展示的代码中,该数组从未初始化过。因此,即使调用.FirstOrDefault之类的函数,最终也会出现NullReferenceException。如果可以的话,最好不要使用Arrays。只要坚持使用IEnumerable

另外,您的linq查询还有一个额外的步骤;与其检查控件集合中每个控件的类型,不如调用其.OfType方法

最后,linq的美妙之处在于,您甚至不必编写for循环。您可以只编写一条语句来计算所有文本框

IEnumerable<int> dreamX;

private void Form1_Load(object sender, EventArgs e)
{ 
    sumX();
    int totalX = dreamX.Sum();
}

private void sumX()
{
    dreamX = from control in Controls.OfType<TextBox>()
             where control.Name.StartsWith("box")
             select Convert.ToInt32(control.Text);
}
你想要的是:

int[] dreamX;
private void Form1_Load(object sender, EventArgs e)
        { 
         sumX();
        }
 private void sumX()
        {
                dreamX =( from Control control in Controls                  
                         where control.GetType() == typeof(TextBox) &&
                               control.Name.StartsWith("box")
                         select Convert.ToInt32(((TextBox)control).Text))
                                       .ToArray();             
        }

from子句生成IEnumerable集合。仅当至少有一个元素:-时,才能将其转换为扩展名为.ToArray的数组。否则它会返回0。嗯,你怎么会得到int dreamX[];编译?这是什么意思?@Joel:让那家伙休息一下!他在学习,显然很享受。