查询词典词典?

请问您如何查询字典词典和/或列表字典?

private Dictionary<string, Dictionary> masterDict= new Dictionary<string, Dictionary>(); Private Dictionary<string, List> masterList= new Dictionary<string, List>(); 

我知道如果我执行以下操作,我会得到masterDict中包含的字典列表,但我不确定如何获取这些字典的值。

  foreach (var kvp in masterDictMethod()) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } 

谢谢你的期待;)

在你的foreach中, kvp.Value是每个masterDict条目的内部字典,即Dictionary

所以,只要kvp.Value ,你就会得到内在价值。

例如

 foreach (var kvp1 in masterDictMethod()) { Console.WriteLine("Key = {0}, Inner Dict:", kvp1.Key); foreach (var kvp2 in kvp1.Value) { Console.WriteLine("Date = {0}, Double = {1}", kvp2.Key, kvp2.Value); } } 

使用masterDict.Values

这个是:

 var masterDictionary = new Dictionary>(); var query = from kvp1 in masterDictionary from kvp2 in kvp1.Value select new {TheString = kvp1.Key, TheDate = kvp2.Key, TheDouble = kvp2.Value }; foreach(var x in query) { Console.WriteLine("{0} {1} {2}", x.TheString, x.TheDate, x.TheDouble); } 

然后另一个是:

 var masterList= new Dictionary>(); var query = from kvp in masterList from val in kvp.Value select new {TheString = kvp.Key, TheDate = val); foreach(var x in query) { Console.WriteLine("{0} {1}", x.TheString, x.TheDate); } 
 foreach (var key in masterDict.Keys) { var nestedDict = masterDict[key]; } 

您询问了包含其他词典的列表,词典和词典。

我最近有一个类似的话题,我想要一个可查询的字典(即允许将查询表达式作为lambda参数传递的扩展方法),你可以使用如下:

 var result = myDictionary.QueryDictionary(w => myList.Any(a => a == w.Key)); 

此代码行的目的是检查myList中是否包含字典的任何键。

所以我做的就是这个,我编写了以下扩展方法:

 // extension method using lambda parameters public static Dictionary QueryDictionary( this Dictionary myDict, Expression, bool>> fnLambda) { return myDict.AsQueryable().Where(fnLambda).ToDictionary(t => t.Key, t => t.Value); } 

它可以用于每个具有string类型的键和每个对象类型T项的字典。

现在,您可以通过传递lambda表达式轻松编写查询,如下例所示:

 var list1 = new List() { "a", "b" }; var myDict = new Dictionary(); myDict.Add("a", "123"); myDict.Add("b", "456"); myDict.Add("c", "789"); var result = myDict.QueryDictionary(w => list1.Any(a => a == w.Key)); 

结果将包含项目a和b,因为它们包含在list1中。

您还可以查询词典字典 ,这里是LinqPad的C#示例,但它也可以用作控制台应用程序(只需注释掉.Dump()语句并用Console.WriteLine(...)语句替换它们):

 void Main() { // *** Set up some data structures to be used later *** var list1 = new List() { "a", "b", "d" }; // a list var myDict = new Dictionary(); // the dictionary myDict.Add("a", "123"); myDict.Add("b", "456"); myDict.Add("c", "789"); var myDict2 = new Dictionary(); // 2nd dictionary myDict2.Add("a", "123"); myDict2.Add("b", "456"); myDict2.Add("c", "789"); myDict.Add("d", myDict2); // add 2nd to first dictionary // *** 1. simple query on dictionary myDict *** var q1 = myDict.QueryDictionary(w => list1.Any(a => a == w.Key)); q1.Dump(); // *** 2. query dictionary of dictionary (q3 contains result) *** var q2 = (Dictionary)q1.QueryDictionary(w => w.Key.Equals("d")).First().Value; var q3 = q2.QueryDictionary(w => w.Key.Equals("b")); q3.Dump(); } // *** Extension method 'QueryDictionary' used in code above *** public static class Extensions { public static Dictionary QueryDictionary( this Dictionary myDict, Expression, bool>> fnLambda) { return myDict.AsQueryable().Where(fnLambda).ToDictionary(t => t.Key, t => t.Value); } } 

由于此解决方案使用Generics,因此您可以将任何lambda表达式作为搜索参数传递,因此它非常灵活。