开发者

public IEnumerable <entityClass> set to Linq query, returns null - Asp.net page

开发者 https://www.devze.com 2023-03-13 15:16 出处:网络
I have a web page that will make frequent calls to a data source. Instead of sending many calls to a database, I want to localize the set and then make the various controls query the local set using l

I have a web page that will make frequent calls to a data source. Instead of sending many calls to a database, I want to localize the set and then make the various controls query the local set using linq. I may be trying to do this all wrong, so I'm looking for some input.

private IEnumerable<entityFrameworkClass> _theList;
private IEnumerable<entityFrameworkClass> theList
{ set { _theList = from i in context select i;} get { return _theList; }}

Then in Page_Load

var yearQuery = from y in theList
                     select y;

I get an error that the source is null during debug.

Any ideas or maybe recommend开发者_运维知识库ations for a better method to accomplish this?


You are initializing the inner private variable _theList in the set of theList property but when reading it, it's the get that will be accessed (Properties in C#).

What you want to do is this (Try also to write properties with CamelCase[Capitalization Conventions]):

private IEnumerable<entityFrameworkClass> TheList
{ 
    get
    { 
        if(_theList == null)
        {
             _theList = from i in context select i;
        }
        return _theList; 
    }
}


In your code above the "set" is never getting called hence your _theList is empty I would also change it to:

_theList = (from i in context select i).ToList();

To ensure it gets called now -without deferred execution

0

精彩评论

暂无评论...
验证码 换一张
取 消