2011-02-10 123 views
0

只是插科打诨,努力扩大我的包O”招数:我只是尝试,并希望这样做Dictionary对象与其他内部字典随着外部字典的.value的嵌套字典对象?

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

对象类型是一个枚举

所以......那么......要么你不想这样做,要么我不知道怎么因为我试图弄清楚如何填充和从中检索数据。

目的可能有所帮助:我正在传递ObjectGUID,需要翻阅一堆数据库表以确定该对象存在于哪个表中。我已经编写的方法只是查询每个表并返回count(此处有几个例子)

  // 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; 

所以,这就是全部完成,并正常工作......但因为字段名和表名是每个改变的唯一的东西检查我开始思考我可以重新使用where重复的代码,我创建了一个字典和一个foreach循环,可以翻转并更改sql行(如下所示)...但是,正如您在下面看到的,我需要该ObjectType作为每个表/字段名对的键我可以返回ñ它没有任何进一步的计算

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爱好者可能把我的一些新的巧妙构思研究......我猜猜别人会打我的头,并告诉我使用阵列... :)

EDIT @ Jon Skeet --------------------- ---------------------

嘿,甜美,想想我可能会找到正确的方式来做到这一点......还没有运行它但这是我为你写的一个例子

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> 0返回ObjectType.Domain ...看起来正确吗?唯一的问题是,虽然它看起来有些聪明,但它像2个字典对象,一对字符串,一些嵌套循环,比我的第一个版本更难读取和调试,并且每次检查约10行以上hehe ...如果这看起来像你那么我想这是一件事我可以添加到我的大脑:)


也发现了这个how to fetch data from nested Dictionary in c#

+0

这是不是很清楚你究竟要问什么... – 2011-02-10 16:27:48

回答

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); 
} 

如果没有帮助,请表明你已经尝试与失败的码 - 中在我的问题中,数据库代码并不真正相关,因为它不会尝试使用嵌套字典。

+0

你只是一个错字..我会修复它 – 2011-02-10 16:27:52