查询字典词典?

问题描述:

请你可以告诉我如何查询字典词典和/或列表字典?查询字典词典?

private Dictionary<string, Dictionary<DateTime, double>> masterDict= new Dictionary<string, Dictionary<DateTime, double>>(); 

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

我知道如果我这样做,我得到包含在masterDict字典的名单,但我不知道如何在这些字典的价值得到。

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

感谢您寻找;)

在你的foreach kvp.Value是每个masterDict入口即Dictionary<DateTime, double>

这样的内部字典,也只是的foreach超过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); 
    } 
} 
+0

完美。谢谢。 – Brian 2010-09-16 11:48:32

使用masterDict.Values

foreach (var key in masterDict.Keys) 
{ 
    var nestedDict = masterDict[key]; 
} 
+0

会更好做:'foreach(masterDict中的var kv){/ *使用kv.Value - 内部字典。 * /}'。这节省了内部查找('Dictionary ' implements'IEnumerable >'。) – Richard 2010-09-16 12:21:06

这一个是:

var masterDictionary = new Dictionary<string, Dictionary<DateTime, double>>(); 

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<string, List<DateTime>>(); 

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

你知道通过扩展方法而不是查询吗? – mggSoft 2012-01-19 10:24:03

+1

@MGG_Soft是的,你正在寻找Enumerable.SelectMany的重载之一 – 2012-01-19 13:18:34

你问到列表,字典等含字典词典。

我最近类似的话题,在这里我想有一个可查询字典(即一个扩展方法,它允许通过一个查询表达式作为拉姆达参数),你可以用这样的:

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

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

因此,我所做的就是这个,我写了下面的扩展方法:

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

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

现在,你可以通过一个lambda表达式,如下面的例子很容易地编写查询:

var list1 = new List<string>() { "a", "b" }; 
var myDict = new Dictionary<string, object>(); 
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,因为它们都包含在列表1。

您也可以查询字典词典的,下面是LinqPad一个C#例子,但它可以作为一个控制台应用程序,以及(只是注释掉.Dump()声明和Console.WriteLine(...)语句代替它):

void Main() 
{ 
    // *** Set up some data structures to be used later *** 
    var list1 = new List<string>() { "a", "b", "d" }; // a list 
    var myDict = new Dictionary<string, object>(); // the dictionary 
    myDict.Add("a", "123"); myDict.Add("b", "456"); myDict.Add("c", "789"); 

    var myDict2 = new Dictionary<string, object>(); // 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<string, object>)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<string, T> QueryDictionary<T>(
     this Dictionary<string, T> myDict, 
     Expression<Func<KeyValuePair<string, T>, bool>> fnLambda) 
    { 
     return myDict.AsQueryable().Where(fnLambda).ToDictionary(t => t.Key, t => t.Value); 
    } 
} 

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