Create Dictionary(of Dictionary) using Grouping in Linq

前端 未结 4 1142
星月不相逢
星月不相逢 2021-01-03 14:07

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 sol

4条回答
  •  醉梦人生
    2021-01-03 14:27

    This more or less Spender's solution, but this works.

    In C#

    var dict = lstTeachers.Zip(lstStudentsSex, (teacher, sex) => new { teacher, sex })
        .Zip(lstStudentName, (x, student) => new { x.teacher, x.sex, student })
        .GroupBy(x => x.teacher)
        .ToDictionary(g => g.Key, g => g.GroupBy(x => x.sex)
            .ToDictionary(p => p.Key, p => p.Select(x => x.student)));
    

提交回复
热议问题