字典< 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:
-
from dictionary in m1
-类似于foreach(var dictionary in m
),只是它不进行迭代(因为它是一个蓝图).它指定我们要遍历的源(m1
)和要分配给每个成员的变量(dictionary
.我们知道它将是Dictionary<String, String>
类型的) -
from keyValuePair in dictionary
-在这里,我们使用从上一条语句创建的dictionary
变量.keyValuePair
的类型将为KeyValuePair<string,string>
,因为执行查询时我们将通过Dictionary<string,string>
迭代". -
where keyvaluePair.Key == "h1"
-这将从Key
属性等于"h1"的前一条语句中筛选出keyValuePairs. - 现在我们过滤掉了
KeyValuePair
,我们可以select
他们的Value
属性.这会将过滤出的KeyValuePair
序列投影"到新的IEnumerable<string>
类型.
- 最后,
ToList
方法执行查询以获取结果.
-
from dictionary in m1
- This is much likeforeach(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 typeDictionary<String, String>
) -
from keyValuePair in dictionary
- Here we use thedictionary
variable created from the previous statement. The type ofkeyValuePair
will beKeyValuePair<string,string>
because we will be "iterating" through aDictionary<string,string>
when the query is executed. -
where keyvaluePair.Key == "h1"
- This filters out the keyValuePairs from the previous statement whoseKey
property equals "h1". - Now that we filtered out the
KeyValuePair
s, we canselect
theirValue
property. This "projects" the filtered outKeyValuePair
sequence to the new typeIEnumerable<string>
- Finally,
ToList
method executes the query to get the results.