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

前端 未结 4 1122
孤城傲影
孤城傲影 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条回答
  •  萌比男神i
    2021-01-19 01:58

    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 :)

提交回复
热议问题