对List中的元素进行分组 [英] grouping the elements within a List

查看:489
本文介绍了对List中的元素进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我给了一个清单

Hi,

I gave a List

Name  Dep  YearofJoin
====================
x     d     2010
y     d     2010

a     g     2010
b     g     2010
c     g     2012
====================





我想分组元素基于Dep然后分组然后基于yearOf加入



我正在使用groupby(dep)但是我无法继续前进。



任何建议。



[edit]已添加代码块 - OriginalGriff [/ edit]



I want to group the elements based on Dep and then group then based on yearOf Joining

I am using groupby(dep) but am not able to move further.

Any suggestion.

[edit]Code block added - OriginalGriff[/edit]

推荐答案

根据您的查询,这些名称将属于一个具有相同Dep和相同YearofJoin的组。因此,从给定的例子中,我们将获得3组(2010_d),(2010_g)和(2012_g)



According to your query, those names would come under one group which has same Dep and same YearofJoin. Therefore, from the given example, we would get 3 groups (2010_d), (2010_g) and (2012_g)

var studentGroups = from s in students
	group s by string.Format("{0}_{1}", s.YearofJoin, s.Dep) into g
	select new { GroupId = g.Key, StudentInfo = g };


foreach (var sg in studentGroups)
{
	foreach (var s in sg.StudentInfo)
	{
	}
}

这篇关于对List中的元素进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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