LINQ:无法隐式转换类型'System.Collections.Generic.IEnumerable< int>' 'int' [英] LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'

查看:87
本文介绍了LINQ:无法隐式转换类型'System.Collections.Generic.IEnumerable< int>' 'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

您想要的是什么

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()的数组

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

这篇关于LINQ:无法隐式转换类型'System.Collections.Generic.IEnumerable&lt; int&gt;' 'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆