字典< string,List< Dictionary< string,string>>使用linq查询 [英] Dictionary<string, List<Dictionary<string,string>>> query using linq

查看:133
本文介绍了字典< string,List< Dictionary< string,string>>使用linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字典

        Dictionary<string,string> q1=new Dictionary<string,string>
        { 
        {"h1","name1"},{"h2","name2"}
        };
        Dictionary<string,string> q2=new Dictionary<string,string>
        { 
        {"h1","name12"},{"h2","name23"}
        };
        Dictionary<string,string> q3=new Dictionary<string,string>
        { 
        {"h1","name123"},{"h2","name234"}
        };
        List<Dictionary<string,string>> m1 = new List<Dictionary<string,string>> { q1,q2,q3 };
        Dictionary<string, List<Dictionary<string,string>>> mhi = new Dictionary<string, List<Dictionary<string,string>>>();
        mhi.Add("x1", m1);

我需要使用linq返回具有值name1,name12,name123的列表. 我知道适用于我的正常方法.但是我很好奇如何使用linq来实现这一点

I need to return a list which has the values name1,name12,name123 using linq. I am aware of normal method which works for me. But I am curious to know how to implement this using linq

推荐答案

尝试一下:

var q1 = new Dictionary<string, string> {
    {"h1", "name1"},
    {"h2", "name2"}
};
var q2 = new Dictionary<string, string> {
    {"h1", "name12"},
    {"h2", "name23"}
};
var q3 = new Dictionary<string, string> {
    {"h1", "name123"},
    {"h2", "name234"}
};
var m1 = new List<Dictionary<string, string>> { q1, q2, q3 };
//Using LINQ
List<string> result = (from dictionary in m1
                       from keyValuePair in dictionary
                       where keyValuePair.Key == "h1" 
                       select keyValuePair.Value).ToList();
//result = name1,name12,name123

//without linq
var result2 = new List<string>();
foreach(var dictionary in m1)
    foreach(var keyValuePair in dictionary)
        if(keyValuePair.Key == "h1")
            result2.Add(keyValuePair.Value);

修改:
from子句指定数据源,where子句应用过滤器,select子句将序列的每个元素投影为新形式.

Edit:
The from clause specifies the data source, the where clause applies the filter, and the select clause projects each element of the sequence into a new form.

Linq查询只有在我们对其进行迭代之前才被执行. (此处.ToList()正在执行此操作).就像一个蓝图,它指定了执行(迭代)时如何返回信息.

Linq queries are not executed until we iterate through it. (Here .ToList() is doing that). It's like a blueprint which specifies how the information is returned when executed(iterated).

让我们分别检查每个语句:

Lets examine each statement separately:

  1. from dictionary in m1-类似于foreach(var dictionary in m),只是它不进行迭代(因为它是一个蓝图).它指定我们要遍历的源(m1)和要分配给每个成员的变量(dictionary.我们知道它将是Dictionary<String, String>类型的)
  2. from keyValuePair in dictionary-在这里,我们使用从上一条语句创建的dictionary变量. keyValuePair的类型将为KeyValuePair<string,string>,因为执行查询时我们将通过Dictionary<string,string>迭代".
  3. where keyvaluePair.Key == "h1"-这将从Key属性等于"h1"的前一条语句中筛选出keyValuePairs.
  4. 现在我们过滤掉了KeyValuePair,我们可以select他们的Value属性.这会将过滤出的KeyValuePair序列投影"到新的IEnumerable<string>
  5. 类型.
  6. 最后,ToList方法执行查询以获取结果.
  1. from dictionary in m1 - This is much like foreach(var dictionary in m) except that it doesn't iterate (Because its a blueprint). It specifies which source we are iterating through (m1) and the variable to assign to each member (dictionary that is. We know that it will be of type Dictionary<String, String>)
  2. from keyValuePair in dictionary - Here we use the dictionary variable created from the previous statement. The type of keyValuePair will be KeyValuePair<string,string> because we will be "iterating" through a Dictionary<string,string> when the query is executed.
  3. where keyvaluePair.Key == "h1" - This filters out the keyValuePairs from the previous statement whose Key property equals "h1".
  4. Now that we filtered out the KeyValuePairs, we can select their Value property. This "projects" the filtered out KeyValuePair sequence to the new type IEnumerable<string>
  5. Finally, ToList method executes the query to get the results.

这篇关于字典&lt; string,List&lt; Dictionary&lt; string,string&gt;&gt;使用linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆