访问词典会话 [英] Accessing dictionary in session

查看:122
本文介绍了访问词典会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有,我想在会话中存储的字典。这本词典将存储对象的名单与时间和日期的关键。

Let's say I have a dictionary that I want to store in the session. This dictionary will be storing a list of object with a date as the key.

Dictionary<DateTime, List<MyObjects>> SessionDictionaryMyObjects = new...

我如何把清单MYLIST在字典中的关键31/1/2011?如何从字典检索列表为2011年1月19日?

How do I put a list MyList in the dictionary with the key 31/1/2011 and how do I retrieve the list for 1/19/2011 from the dictionary?

感谢。

推荐答案

喜欢这个?

Dictionary<DateTime, List<MyObjects>> SessionDictionaryMyObjects = Session["SessionDictionaryMyObjects"] as Dictionary<DateTime, List<MyObjects>>;
if (SessionDictionaryMyObjects == null)
{
     Session["SessionDictionaryMyObjects"] = SessionDictionaryMyObjects = 
          new Dictionary<DateTime, List<MyObjects>>();
}

// Set value
SessionDictionaryMyObjects.Add(new DateTime(2011, 1, 31), yourListObject);

if (SessionDictionaryMyObjects.Contains(new DateTime(2011, 1, 19)))
{
    // Get value
    List<MyObjects> o = SessionDictionaryMyObjects[new DateTime(2011, 1, 19)];
}

您还陶锡源应该如果与字典中存在的值的containsKey

You also should sheck if the value exists in the dictionary with ContainsKey method

这篇关于访问词典会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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