我正在尝试使用LINQ表达式打印一行数字(2、4、8、16、32),但这些数字应该大于10但小于1000。我不知道我做错了什么。
当我使用from时,程序中出现错误,在r下面有下划线。我不理解这个错误的含义。
program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3._4
{
class Program
{
static void Main(string[] args)
{
Reeks r = new Reeks();
var query =
from i in r// error is here
where i > 10 && i < 1000
select 2 * i;
foreach (int j in query)
{
Console.Write(j);
}
}
}
}
Reeks.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3._4
{
class Reeks : IEnumerable
{
private int i = 1;
public Reeks() { }
public IEnumerator GetEnumerator()
{
while (true)
{
i = i * 2;
yield return i;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
Reeks
中实现IEnumerable<int>
和非泛型版本。 - Lee