代码仅适用于断点

时间:2015-04-18 00:57:44

标签: c# asp.net

我在会话中存储数据库中的值,如下所示:

private List<Message> _AllMessages = null;
        public List<Message> AllMessages
        {
            get
            {
                _AllMessages = Session["AllMessages"] as List<Message>;
                if (_AllMessages==null)
                {
                    Session["AllMessages"] = LoadMessages();
                }
                return _AllMessages;
            }
            set
            {
                Session["AllMessages"] = value;
            }
        }

当我像这样访问AllMessages时:

AllMessages.Where(y => y.Created.Date == DateTime.Today).ToList();

我收到以下错误:

Value cannot be null.

但是当我在AllMessages.Where行上放置断点时,它似乎将AllMessages评估为Null一秒钟,但随后取值并且一切正常。为什么会这样?

1 个答案:

答案 0 :(得分:1)

我认为你有错误,试试这个:

public List<Message> AllMessages
        {
            get
            {
                _AllMessages = Session["AllMessages"] as List<Message>;
                if (_AllMessages==null)
                {
                    Session["AllMessages"] = LoadMessages();
                    _AllMessages = Session["AllMessages"];
                }
                return _AllMessages;
            }
            set
            {
                Session["AllMessages"] = value;
            }
        }