C# LINQ:无法将类型“System.Collections.Generic.IEnumerable<int>”隐式转换为“int”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1487072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
提问by Darkmage
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'"
“无法将类型 'System.Collections.Generic.IEnumerable -int-' 隐式转换为 'int'”
采纳答案by Dabblernl
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
from 子句生成一个 IEnumerable集合。您可以使用 .ToArray() 扩展名将其转换为数组
回答by Natrium
FirstOrDefault()
will turn the IEnumerable<int>
into an int
.
FirstOrDefault()
将转IEnumerable<int>
成int
。
Actually it takes the first occurance in the outputresult of your linq-query.
实际上,它在 linq-query 的输出结果中第一次出现。
回答by Joey
Well, that query might return more than one value, so you need to either use the .Single()
, .First()
or .FirstOrDefault()
extension methods.
好吧,该查询可能返回多个值,因此您需要使用.Single()
,.First()
或.FirstOrDefault()
扩展方法。
Note, that Single()
will only work if there is exactly oneelement in the list, First()
will only work if there is at least oneelement in the list. FirstOrDefault()
reverts to the default value (0) if there is no element in the list.
请注意,这Single()
仅在列表中只有一个元素First()
时才有效,只有在列表中至少有一个元素时才有效。FirstOrDefault()
如果列表中没有元素,则恢复为默认值 (0)。
Depending on what exactly you need you will have to choose :)
根据您的需要,您将不得不选择:)
回答by Joel Coehoorn
So many things wrong with that.
很多事情都错了。
First of all, you're trying to assign what is potentially manyconverted 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.
此外,您显示的代码中没有任何地方初始化过该数组。所以即使你调用类似的东西,.FirstOrDefault()
你最终也会得到一个NullReferenceException
. 如果可以的话,最好不要使用 arrarys。只需坚持使用 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.
此外,您的 linq 查询还有一个额外的步骤;您应该调用其.OfType()
方法,而不是检查 Controls 集合中每个控件的类型。
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.
最后,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);
}