C# 公共IEnumerable<;实体类>;设置为Linq查询,返回null-Asp.net页面

C# 公共IEnumerable<;实体类>;设置为Linq查询,返回null-Asp.net页面,c#,asp.net,linq,ienumerable,C#,Asp.net,Linq,Ienumerable,我有一个网页,可以经常调用数据源。我不想向数据库发送许多调用,而是希望对集合进行本地化,然后让各种控件使用linq查询本地集合。我可能想把这一切都搞错了,所以我在寻找一些信息 private IEnumerable<entityFrameworkClass> _theList; private IEnumerable<entityFrameworkClass> theList { set { _theList = from i in context select i;}

我有一个网页,可以经常调用数据源。我不想向数据库发送许多调用,而是希望对集合进行本地化,然后让各种控件使用linq查询本地集合。我可能想把这一切都搞错了,所以我在寻找一些信息

private IEnumerable<entityFrameworkClass> _theList;
private IEnumerable<entityFrameworkClass> theList
{ set { _theList = from i in context select i;} get { return _theList; }}
在调试期间,我得到一个源为空的错误

有没有更好的方法来实现这一点的想法或建议?

在您的代码中,上面的“set”永远不会被调用,因此您的列表是空的 我还想把它改为:

_theList = (from i in context select i).ToList(); _theList=(从上下文中的i选择i.ToList();
为了确保现在调用它—不延迟执行

您正在初始化列表属性的
集合
中的内部私有变量
,但在读取它时,将访问它()

您要做的是(尝试使用CamelCase编写属性):

私有IEnumerable列表
{ 
收到
{ 
如果(\u theList==null)
{
_列表=从上下文中的i选择i;
}
返回列表;
}
}

Personal Preference Warning:
var yearQuery=from y在列表中选择y
比需要的时间长很多(而且不必要地冗长)。只要
var yearQuery=theList
就可以了。 _theList = (from i in context select i).ToList();
private IEnumerable<entityFrameworkClass> TheList
{ 
    get
    { 
        if(_theList == null)
        {
             _theList = from i in context select i;
        }
        return _theList; 
    }
}