LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'int'

前端 未结 4 1007
孤城傲影
孤城傲影 2021-01-19 01:19

I get an error from the following code:

int dreamX[];

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

private void sumX()
{
    for (v         


        
4条回答
  •  被撕碎了的回忆
    2021-01-19 02:19

    What you want is this:

    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();             
            }
    

    The from clause produces a IEnumerable collection. You can convert this to an array with the .ToArray() extension

提交回复
热议问题