嵌套字典对象?

时间:2011-02-10 16:24:31

标签: c# arrays dictionary

只是乱搞,试图扩展我的包诀:我只是在尝试,想要做一个类似于Dictionary对象的东西,另一个内部词典作为外部词典的.Value

var dictionary = new Dictionary<ObjectType, Dictionary<string, string>>();

ObjectType是一个枚举

所以......那么......要么你不想这样做,要么我不知道怎么回事?因为当我试图找出如何填充和检索数据时我开始碰壁从它。

目的可能会有所帮助:我正在传递一个ObjectGUID,需要翻阅一堆数据库表来确定该对象所在的表。我已编写的方法只查询每个表并返回计数(这里是几个例子)

            // Domain Check
        sql = string.Format(@"select count(domainguid) from domains where domainguid = ?ObjectGUID");
        count = (int)MySQLHelper.ExecuteScalar(ConnectionStrings.ConnectionStrings.V4DB_READ, sql, pObjectGUID).ToString().Parse<int>();
        if (count > 0)
            return ObjectType.Domain;

        // Group Check
        sql = string.Format(@"select count(domaingroupguid) from domaingroups where domaingroupguid = ?ObjectGUID");
        count = (int)MySQLHelper.ExecuteScalar(ConnectionStrings.ConnectionStrings.V4DB_READ, sql, pObjectGUID).ToString().Parse<int>();
        if (count > 0)
            return ObjectType.Group;

所以,这一切都已经完成并且工作正常......但是因为字段名和表名是每次检查唯一改变的东西我开始考虑在哪里可以重复使用重复代码,我创建了一个字典和一个转换的foreach循环并更改了sql行(如下所示)...但是,正如您在下面看到的那样,我需要将ObjectType作为每个表/ fieldname对的键的一种,这样我就可以返回它而无需进一步计算< / p>

Dictionary<string, string> objects = new Dictionary<string,string>();
            objects.Add("domains", "domainguid");
            objects.Add("domaingroups", "domaingroupguid");
            objects.Add("users", "userguid");
            objects.Add("channels", "channelguid");
            objects.Add("categorylists", "categorylistguid");
            objects.Add("inboundschemas", "inboundschemaguid");
            objects.Add("catalogs", "catalogguid");

            foreach (var item in objects)
            {
                sql = string.Format(@"select count({0}) from {1} where {0} = ?ObjectGUID", item.Value, item.Key);
                count = (int)MySQLHelper.ExecuteScalar(ConnectionStrings.ConnectionStrings.V4DB_READ, sql, pObjectGUID).ToString().Parse<int>();
                if (count > 0)
 return ?????
                }

这并不是那么重要,因为我的原始方法工作得很好,但我认为StackOverflow极客可能会让我转向一些新的聪明的想法来研究......我猜有人会打击我的头脑并告诉我使用数组... :)

编辑@ Jon Skeet ------------------------------------------ < / p> 嘿,甜蜜,以为我可能会以正确的方式去做...还没有运行它但这是我为你写的一个例子

var objectTypes = new Dictionary<string, string>();
        objectTypes.Add("domainguid", "domains");
        var dictionary = new Dictionary<ObjectType, Dictionary<string, string>>();
        dictionary.Add(ObjectType.Domain, objectTypes);

        foreach(var objectType in dictionary)
        {
            foreach(var item in objectType.Value)
            {
                sql = string.Format(@"select count({0}) from {1} where {0} = ?ObjectGUID", item.Key, item.Value);
                count = (int)MySQLHelper.ExecuteScalar(ConnectionStrings.ConnectionStrings.V4DB_READ, sql, pObjectGUID).ToString().Parse<int>();
                if (count > 0)
                    return objectType.Key;
            }
        }

这个块应该在域表中找到domainguid,如果count> gt; 0返回ObjectType.Domain ...看起来对吗?唯一的问题是,虽然它可能看起来有点聪明,但它就像2个字典对象,几个字符串,一些嵌套循环,比我的第一个版本更难阅读和调试,每次检查大约还有10行hehe ...虽然实验很有趣如果这看起来像你,那么我想这是我可以添加到我脑中的另一件事:)


也找到了这个how to fetch data from nested Dictionary in c#

1 个答案:

答案 0 :(得分:4)

你绝对可以这样做,虽然你目前缺少一个结束尖括号和圆括号。它应该是:

var dictionary = new Dictionary<ObjectType, Dictionary<string, string>>().

要添加给定值,您可能需要以下内容:

private void AddEntry(ObjectType type, string key, string value)
{
    Dictionary<string, string> tmp;
    // Assume "dictionary" is the field
    if (!dictionary.TryGetValue(type, out tmp))
    {
        tmp = new Dictionary<string, string>();
        dictionary[type] = tmp;
    }
    tmp.Add(key, value);
}

如果这没有帮助,请显示您尝试过但未通过的代码 - 据我所知,您问题中的数据库代码并不真正相关,因为它不会尝试使用嵌套字典。