Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

I get an error from the following code:

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

I'm getting this error, how do explicit convert this.

"Cannot implicitly convert type 'System.Collections.Generic.IEnumerable -int-' to 'int'"

like image 540
Darkmage Avatar asked Sep 28 '09 13:09

Darkmage


4 Answers

Well, that query might return more than one value, so you need to either use the .Single(), .First() or .FirstOrDefault() extension methods.

Note, that Single() will only work if there is exactly one element in the list, First() will only work if there is at least one element in the list. FirstOrDefault() reverts to the default value (0) if there is no element in the list.

Depending on what exactly you need you will have to choose :)

like image 138
Joey Avatar answered Oct 19 '22 20:10

Joey


So many things wrong with that.

First of all, you're trying to assign what is potentially many converted integers to a single integer within an array. That's what the error message is telling you.

Additionally, nowhere in the code you showed is that array ever initialized. So even if you call something like .FirstOrDefault() you'll end up with a NullReferenceException. Best not to use arrarys at all if you can help it. Just stick with IEnumerable.

Also, you're linq query has an extra step; rather than checking the type of each control in the Controls collection you should call its .OfType() method.

Finally, the beauty of linq is that you don't even have to write the for loop. You can just write one statement that evaluates all your textboxes.

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);
}
like image 32
Joel Coehoorn Avatar answered Oct 19 '22 20:10

Joel Coehoorn


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

like image 38
Dabblernl Avatar answered Oct 19 '22 19:10

Dabblernl


FirstOrDefault() will turn the IEnumerable<int> into an int.

Actually it takes the first occurance in the outputresult of your linq-query.

like image 3
Natrium Avatar answered Oct 19 '22 20:10

Natrium