我想知道是否有可能从字典值中创建一个列表,其中键是一个指定值?
字典是这样的:
Sidcup - DPC1 Sidcup - DPC2 Blackheath - DPC3 Blackheath - DPC4 Bexleyheath - DPC5
事实上,我并不是完全像上面那样实现一个字典是个好主意。下面是它的实现:
DataSet ds = EngineBllUtility.GetDPCsForImportFile(connectionString, fileID);
if (ds.Tables.Count > 0)
{
DataTable dtDPCs = EngineBllUtility.GetDPCsForImportFile(connectionString, fileID).Tables[0];
Dictionary<string, string> preliminaryList = new Dictionary<string, string>();
if (dtDPCs.Columns.Contains("DPCNumber") && dtDPCs.Columns.Contains("BranchName"))
foreach (DataRow dataRow in dtDPCs.Rows)
{
preliminaryList.Add(dataRow["BranchName"].ToString(), dataRow["DPCNumber"].ToString());
}我有以下代码:(请原谅最后一行,这只是为了让你知道我想要做什么)。
foreach (string branch in branchNames)
{
string subfolder = System.IO.Path.Combine(saveLocation, branch);
System.IO.Directory.CreateDirectory(subfolder);
List<string> certificateList = new List<string>();
certificateList.Add(DPCNumber in preliminaryList where Key = branch);
}在上面的代码中,分支是字典中的关键字。我需要遍历,因为它需要创建一个新文件夹,然后对我正在创建的certificateList执行一些操作。
发布于 2012-01-06 18:28:46
当然:
private static void TestZip()
{
Dictionary<string, string> stringstringdic = new Dictionary<string, string>();
stringstringdic.Add("1", "One");
stringstringdic.Add("2", "Two");
stringstringdic.Add("3", "Three");
stringstringdic.Add("4", "Four");
stringstringdic = stringstringdic.Where(pair => pair.Key != "1")
.ToDictionary(pair => pair.Key, pair => pair.Value);
List<string> stringlist = stringstringdic.Keys.Concat(stringstringdic.Values).ToList();
foreach (string str in stringlist)
{
Console.WriteLine(str);
}
}
//Output:
//2
//3
//4
//Two
//Three
//Four当然,您必须更改Where子句以反映您的实际需求。
如果我没理解错的话,就像.Where(pair => pair.Key == branch)
https://stackoverflow.com/questions/8756111
复制相似问题