簡體   English   中英

C# 在列表中動態創建字典列表並填充/檢索值

[英]C# Make List of dictionaries in List dynamically and populate/retrieve values

我讀了一個參數文件,它有幾個部分,每個部分下都有一個參數、一個等號和一個值。 例如:

[users]
Nick=twelve
John=eleven
Mary=twentynine

[stations]
pc1=on
pc2=on
pc3=off
pc4=off

[grades]
a=distinction
b=good
c=pass

etc..

我正在嘗試制作一個字典列表,因為根據定義,我沒有重復的參數名稱(例如,參數“pc1”可以出現在任何部分,但只能出現在一個部分中 - 例如上面,它存在於部分“ [站]”等)並填充和檢索數據。

例如:

MyList.Add(new Dictionary<string, string>()); // "users" - how do I name the dictionary dynamically, as I read the section name?
MyList.Add(new Dictionary<string, string>()); // "stations"

等等

我試圖實現的目標是查詢列表,知道字典名稱和鍵,如果鍵存在則檢索值。

string JohnCode = MyList("users", "John")    //if key "John" exists in users Dictionary

注意:我事先不知道名稱以及我的數據中有多少部分。 我所知道的是,部分在括號中,即 [namehere] 然后總是遵循上面的鍵值對。假設每個鍵對都是一個字符串。

你能幫助我嗎? 先感謝您。

我將假設您的部分和鍵只允許某些字符。 使用它對您有利並制作一個字典,其中鍵是文件部分和鍵的組合。

在本例中,我將使用 pipe | 分隔字典鍵中的段。

var data = new Dictionary<string, string>();
data.Add("users|Nick", "twelve");
//...
data.Add("stations|pc1", "on");
//...
data.Add("grades|a", "distinction");
//...

現在,當您要查找條目時,您可以按部分和鍵(“users|Nick”)查找。

然而,這要求所有條目都是一個部分的一部分。 如果需要,當您閱讀 ini 文件時,您可以保留一個單獨的部分名稱列表,以幫助您稍后進行查找。


雖然它比每個部分都有單獨的字典要貴一些,但您仍然可以查詢數據。

要獲得所有部分的不同列表:

var sectionNames = data
    .Select(x => x.Key.Split('|')[0])
    .Distinct();

獲取部分中的所有條目

var sectionName = "users";
var entriesInUsersSection = data
    .Where(x => x.Key.StartsWith(sectionName + "|"));

並且可以進行更多 Linq 操作。

如果您想要一個唯一的Dictionary<string, string>來存儲每個部分的值,如另一個答案的評論中所示,是否有任何理由必須使用List<Dictionary<string, string>> 因為您似乎可以使用Dictionary<string, Dictionary<string, string>>來解決這個問題。

Dictionary<string, Dictionary<string, string>>key是節名。 value將是Dictionary<string, string>包含該部分下的值。

Dictionary<string, Dictionary<string, string>> sections = 
    new Dictionary<string, Dictionary<string, string>>();

sections.Add("users", new Dictionary<string, string>>());
sections.Add("stations", new Dictionary<string, string>>());
sections["users"].Add("Nick", "twelve");
sections["stations"].Add("pc1", "on"); 

string Nick = sections["users"]["Nick"]; // Nick = "twelve"
string pc1 = sections["stations"]["pc1"]; // pc1 = "on"

您可以使用Dictionary<string, Dictionary<string, string>>擴展方法來安全地處理搜索。 例如:

public static class IniExtensions
{
    public static bool TryGetSectionValue(
        this Dictionary<string, Dictionary<string, string>> src,
        string section, string key, out string value)
    {
        value = null;
        if (src.TryGetValue(section, out Dictionary<string, string> sectionValues))
        {
            return sectionValues.TryGetValue(key, out value);
        }
        return false;
    }
}

Dictionary<string, Dictionary<string, string>> sections = 
    new Dictionary<string, Dictionary<string, string>>();

sections.Add("users", new Dictionary<string, string>>());
sections.Add("stations", new Dictionary<string, string>>());
sections["users"].Add("Nick", "twelve");
sections["stations"].Add("pc1", "on"); 

// This returns true, and the Nick variable will have a value of "twelve"
sections.TryGetSectionValue("users", "Nick", out string Nick);

// this returns false, and the value variable has a value of null
sections.TryGetSectionValue("No Section", "Nick", out string value);

// this returns false, and the John variable has a value of null
sections.TryGetSectionValue("users", "John", out string John);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM