Linq中使用分组创建字典(词典) [英] Create Dictionary(of Dictionary) using Grouping in Linq

查看:225
本文介绍了Linq中使用分组创建字典(词典)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以循环(作为事实上做到这一点,我目前的,但我想学习/ imporve我的LINQ的技能,我也希望它会提供一个比较有效的解决方案。所以,这里是我的方案

I know I could do this with loops (as a matter of fact, I currently am, but I am trying to learn / imporve my Linq skills and i also hope it will provide a more efficent solution. So, here's my scenario:

说我有以下3个列表(我只是做了一个类似的例子,所以请原谅它的愚蠢):

Say I have the following 3 lists (I'm just making up a similar example, so forgive it's stupidity):

Dim lstTeachers as New List(of string)
Dim lstStudentsSex as New List(of string)
Dim lstStudentName as New List(of string)

和它们是,例如,如下:

And they are, for example, as follows:

lstTeachers:     lstStudentsSex:     lstStudentName:
Teacher 1        Male                Whatever Name 1
Teacher 2        Female              Whatever Name 2 
Teacher 1        Female              Whatever Name 3
Teacher 1        Female              Whatever Name 4
Teacher 2        Male                Whatever Name 5
Teacher 3        Male                Whatever Name 6
Teacher 3        Female              Whatever Name 7
Teacher 1        Male                Whatever Name 8
Teacher 1        Female              Whatever Name 9
Teacher 2        Male                Whatever Name 10

凡在每个列表中的每个条目匹配具有相同索引的其他条目 - 基本上就像一个数据表,只存储在单独的列表

Where each entry in each list matches the other entries with the same index - Basically like a datatable, just stored in separate lists.

现在,假设我想用下面的值创建以下结构:

Now, suppose I want to create the following structure with the following values:

Dim dictTeacherSexName as New Dictionary(Of String, Dictionary(Of String, List(of String)))

Dict1_Key:        Dict1_Value / Dict2_Key:         Dict2_Value:
Teacher 1         Male                             Whatever Name 1
                                                   Whatever Name 8
                  Female                           Whatever Name 3
                                                   Whatever Name 4
                                                   Whatever Name 9
Teacher 2 ...

...我希望这说明了什么,我希望实现的。

... I hope this explains what I'm looking to accomplish.

现在,再一次,我知道这可能是一个愚蠢的想法,但是我也问,因为我想提高我的LINQ的技能 - 特别是GROUP BY和多种选择仍然抓住了我,所以请大家帮忙。

Now, again, I know this may be a stupid idea, but I'm also asking it because I want to improve my Linq skills - especially Group By and multiple Selects which still catches me, so please help.

太感谢了!

推荐答案

三个列表让事情变得有点困难,但并非不可能。要么你先压缩名单在一起所建议的@fsimonazzi,或您基于列表索引查询,而不是列表条目本身。其结果可能是这样的:

The three lists make things a bit difficult, but not impossible. Either you first zip the lists together as suggested by @fsimonazzi, or you base your query on the list indices rather than the list entries themselves. The result could look like this:

<子>的(C#)

var result = Enumerable
    .Range(0, lstTeachers.Count)
    .GroupBy(i => lstTeachers[i])
    .ToDictionary(g => g.Key, g => g
        .GroupBy(i => lstStudentsSex[i])
        .ToDictionary(h => h.Key, h => h
            .Select(i => lstStudentName[i])
            .ToList()));

// result is Dictionary<string, Dictionary<string, List<string>>>

这篇关于Linq中使用分组创建字典(词典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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