如何在C#中检索嵌套字典中最近添加的密钥 [英] How to retreive recently added key in nested dictionary in C#

查看:84
本文介绍了如何在C#中检索嵌套字典中最近添加的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Storage类中有一个嵌套字典为



private static Dictionary< Guid,Dictionary< string,object>>商店{get;设置;}



我有另一个名为Handler.cs的类,其中我在存储类的静态字段中为GUID键添加值为



I have a nested dictionary in my Storage class as

private static Dictionary<Guid, Dictionary<string, object>> Store {get; set;}

I have another class named Handler.cs in which I add value to GUID key in static field of Storage class as

Guid clientId = Guid.NewGuid();
Session.SessionStore.Add(clientId, new Dictionary<string, object>());





我有另一个名为Executor.cs的类,其中我在Storage.cs类中调用add函数,根据Handler.cs中添加的GUID将值添加到Storage类中静态字段的子字典中





I have another class named Executor.cs in which I call add function in Storage.cs class to add values to sub dictionary of static field in Storage class based on GUID added in Handler.cs as

public Session sessionObject;
public void saveSession(HttpListenerContext contxt)
{
this.Storage.Add("Person", objPerson);
}



在Storage.cs我有功能添加为




In Storage.cs I have function Add as

public void Add(string key,object value)
        {
            
            //foreach (var item in SessionStore.Keys)
           // {
               
               // SessionStore[item].Add(key, value);
           // }
        }



但是在我的添加函数中,我只能传递子字典的键和值。我已经在Handler.cs中添加了GUID键。所以从字典列表中我应该如何找到最近添加的GUId?



我尝试过的事情:



我为GUID添加了值,并希望为最近添加的Guid


But in my add function I can pass only key and value of sub dictionary. I have already added the GUID key in Handler.cs. So from the dictionary list how should I find the recently added GUId?

What I have tried:

I added value to GUID and want to add sub dictionary values for the recently added Guid

推荐答案

>添加子字典值。所以从字典列表中我应该如何找到最近添加的GUId?

这将是 Store.Keys 中的最后一个条目。



话虽如此,我不建议记住最近添加的 Guid 密钥,因为它引入了不必要的限制如何商店必须使用。 相反,make Storage.Add()明确接受 Guid 键。



/ ravi
> So from the dictionary list how should I find the recently added GUId?
This will be the last entry in Store.Keys.

That being said, I don't recommend "remembering" the most recently added Guid key as it introduces an unnecessary restriction in how Store must be used.  Instead, make Storage.Add() explicitly accept the Guid key.

/ravi


这篇关于如何在C#中检索嵌套字典中最近添加的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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