尽管在同一方法中对象是可访问的,但在Linq语句中无法识别对象

时间:2013-07-27 11:10:50

标签: c# linq

我在Linq语句中有一个对象“word”(参见:“来自bee.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;
    ......
}

2 个答案:

答案 0 :(得分:5)

你的变量是word,你放world

 from bee in world.Bees //should be word.Bees

注意:就我个人而言,我会将您的word变量重构为world ...(为此,请右键单击单词并选择重构&gt;重命名)

答案 1 :(得分:4)

你在这里犯了一个错误。

将您的变量世界更改为单词

 from bee in world.Bees

from bee in word.Bees