c# - 这是方法体中多键字典的有效代码吗?

标签 c# .net-3.5 dictionary

我只想问:

  1. 下面的代码效率高吗?
  2. 有更好的方法来处理这个问题吗?
  3. 如果需要表名/字段名对的附加值,如何编码?

我们需要使用一个多键字典,其中包含类似 (TableName, FieldName, FieldValue) 的内容。

我搜索了一些答案,但到目前为止我找到的答案不适用于我们的设置。我们正在使用 3.5,所以还没有可用的元组。我们还将此脚本逻辑与仅允许在方法主体“内部”编码的应用程序集成,因此我们受到限制,无法创建单独的类/结构等。我们的设置是 C#/VS 2010。

感谢任何帮助。提前致谢!

Dictionary<string, Dictionary<string, string>> tableList = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> fieldList = new Dictionary<string, string>();

// add fields to field list, then add the field lists to the corresponding table list
// clear field list for next table
// values are just hardcoded here to simplify, but is being read from actual objects in the application

fieldList.Add("Field1", "abc");
fieldList.Add("Field2", "def");
fieldList.Add("Field3", "ghi");
fieldList.Add("Field4", "jkl");

tableList.Add("Table1", new Dictionary<string, string>(fieldList));
fieldList.Clear();

fieldList.Add("Field1", "xyz");
fieldList.Add("Field2", "uvw");
fieldList.Add("Field3", "rst");

tableList.Add("Table2", new Dictionary<string, string>(fieldList));
fieldList.Clear();

fieldList.Add("Field1", "123");
fieldList.Add("Field2", "456");

tableList.Add("Table3", new Dictionary<string, string>(fieldList));
fieldList.Clear();

// Display tables and corresponding fields                          

foreach (KeyValuePair<string, Dictionary<string, string>> fieldList4 in tableList)
{
    foreach (KeyValuePair<string, string> fieldList5 in fieldList4.Value)
    {
        txtMessage.Text = txtMessage.Text + "\r\nTable=" + fieldList4.Key + ", Field=" + fieldList5.Key + " - " + fieldList5.Value;
    }
}

// Try to find tables and fields in the lists, and list the value if found

string tableToFind = "Table2";
string fieldToFind = "Field2";
Dictionary<string, string> tableFields = new Dictionary<string, string>();

if (tableList.Keys.Contains(tableToFind) == true)
{
    txtMessage.Text = txtMessage.Text = "\r\nTable=" + tableToFind + " exist in table list";
    tableList.TryGetValue(tableToFind, out tableFields);

    if (tableFields.Keys.Contains(fieldToFind) == true)
    {
        foreach(KeyValuePair<string, string> fieldData  in tableFields)
        {
            if (fieldData.Key == fieldToFind)
            {
                txtMessage.Text = txtMessage.Text + "\r\nTable=" + tableToFind + ", Field=" +  fieldData.Key + 
                                                    " with value=" + fieldData.Value + " exist in table list";
                break;
            }
        }
    }
}

最佳答案

您可以使用编译器为您创建复合键:使用匿名类型。

var dictionary = new Dictionary<Object, int>();

dictionary.Add(new{Text="A", Number=1}, 1);
dictionary.Add(new{Text="A", Number=2}, 3);
dictionary.Add(new{Text="B", Number=1}, 4);
dictionary.Add(new{Text="B", Number=2}, 5);

var x = dictionary[new{Text="B", Number=2}];

C# 将根据您的字段实现 Equals 和 GetHashcode。因此,您确实得到了一把 key ,它的行为与您预期的一样。

关于c# - 这是方法体中多键字典的有效代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7394463/

相关文章:

c# - 匹配组中特定单词模式不同值的正则表达式拆分字符串

c# - 在 Windows 10 上打开手电筒

python - 使用 for 循环填充字典

python - 正在构造 key :Value pair from list comprehension in Python

c# - Datatable to dictionary 几列

c# - 全局访问单例的Silverlight应用资源

c# - 当 C# 函数参数包含 `= null` 时,这意味着什么?

c# - 使用 ASP.NET 表单例份验证,如何让图像显示在登录屏幕上?

c# - LINQ子查询 "NOT IN"问题

c# - 为什么绑定(bind)值在使用 EndCurrentEdit 时第一次更改时不保留到数据源?