我在 Linq 语句中有一个对象“单词” (请参阅:“ from bee in world.Bees”),尽管在同一方法中该对象在前一行中被识别,但该对象“单词”未被识别。我现在不明白为什么会这样。我在我的代码中到处使用这个词对象没有问题。我对代码做了一些评论以突出问题......
以下是相关的代码片段:
public partial class Form1 : Form
{
private World word;
public Form1()
{
InitializeComponent();
word = new World(new BeeMessage(SendMessage)); // BeeMessage is a delegate
.......
}
private void SendMessage(int ID, string Message)
{
int count = word.Bees.Count; //this line works !! now error message
// LinQ selection
var beeGroups =
from bee in world.Bees // The item word does not exists in the current context
group bee by bee.CurrentState into beeGroup
orderby beeGroup.Key
select beeGroup;
..............
}
[Serializable]
class World
{
public List<Bee> Bees;
......
}