查询字典词典?

时间:2010-09-16 11:01:46

标签: c# .net linq list dictionary

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

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 个答案:

答案 0 :(得分:6)

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

所以,也可以通过kvp.Value来获得内在价值。

e.g。

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

答案 1 :(得分:1)

使用masterDict.Values

答案 2 :(得分: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);
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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

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,因为它们包含在list1中。

您还可以查询字典词典,这里是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);
    }
}

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