2010-09-16 243 views
7

請你可以告訴我如何查詢字典詞典和/或列表字典?查詢字典詞典?

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

感謝您尋找;)

回答

5

在你的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

0
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

1

這一個是:

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

0

你問到列表,字典等含字典詞典。

我最近類似的話題,在這裏我想有一個可查詢字典(即一個擴展方法,它允許通過一個查詢表達式作爲拉姆達參數),你可以用這樣的:

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表達式作爲搜索參數,因此它非常靈活。