ASP.net会话,引用或不?..和如何编写会召开一个列表? [英] ASP.net Sessions, references or not?..and how to write a session holding a list?

查看:85
本文介绍了ASP.net会话,引用或不?..和如何编写会召开一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个属性在我的code:

I have this property in my code:

public List<TreeViewNodeContentHolder> NodeContentHolder
    {
        get
        {
            if (Session["NodeContainerSession"] == null)
            {
                var tempSessionVar = new List<TreeViewNodeContentHolder>();
                Session["NodeContainerSession"] = tempSessionVar;
                return (List<TreeViewNodeContentHolder>)Session["NodeContainerSession"];
            }
            else
                return (List<TreeViewNodeContentHolder>)Session["NodeContainerSession"];
        }
    }

和我想才达到的事情是,我希望能够添加对象和LINQ查询列表/会话。
所以我basicly喜欢对子级要能写类似:

and the thing I want to achive is that I want to be able to add objects and linq-query the list/session. So basicly I whould like to be able to write something like:

NodeContentHolder.Add(new TreeViewNodeContentHolder{prop1=1,prop2=2});

我也希望能够做到这一点:

I also want to be able to do this:

var someNode = NodeContentHolder.Where(e=>e.prop1 == x).FirstOrDefault();

然后另外一个问题aswell..if我做对子级上面这行的事...我对子级然后就可以做,以更新会话对象:

And then another question aswell..if I whould do the thing above this line...whould I then be able to update the object in the session by doing:

someNode.prop1 = 12345;

...

或者我有对子级先拉的对象,然后从列表中/会话删除它,然后重新添加它,确保它被更新?..

Or whould I have to first "pull" the object and then remove it from the list/session and then add it back in to make sure that it gets updated?..

在此先感谢!

推荐答案

所有的,你说这里是正确的,你在这里提到的名单是保存参照,如果你继续区段的引用则会话保存最后,在页面卸载,所以任何更改名单上反映了对象,会话将被保存。

All of that you say here is correct, all the list you mention here are save by reference, and if you keep on session the reference then the session is saved last, on page unload, so anything you change on the list is reflected on the object that session will be save.

唯一想我会改变你的code是如果会话[NodeContainerSession]!= NULL 不是这个意思,这也是一个列表&LT; TreeViewNodeContentHolder方式&gt; ,这是一个检查,你必须让

The only think that I will change on your code is if Session["NodeContainerSession"] != null did not mean that is also a List<TreeViewNodeContentHolder>, this is a check that you must make.

public List<TreeViewNodeContentHolder> NodeContentHolder
{
    get
    {
        object oSessionNode = Session["NodeContainerSession"] as List<TreeViewNodeContentHolder>;

        if (oSessionNode == null)
        {
            oSessionNode = new List<TreeViewNodeContentHolder>();
            Session["NodeContainerSession"] = oSessionNode;
        }

        return (List<TreeViewNodeContentHolder>)oSessionNode;
    }
}

这篇关于ASP.net会话,引用或不?..和如何编写会召开一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆