class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student{Name="aaa",ClassName="a",Age=20,StuNo="a001"},
new Student{Name="bbb",ClassName="a",Age=18,StuNo="a003"},
new Student{Name="ccc",ClassName="b",Age=20,StuNo="b001"},
new Student{Name="ddd",ClassName="b",Age=20,StuNo="b045"},
new Student{Name="eee",ClassName="c",Age=20,StuNo="c001"},
new Student{Name="fff",ClassName="c",Age=20,StuNo="c008"},
new Student{Name="ggg",ClassName="c",Age=20,StuNo="c050"},
new Student{Name="hhh",ClassName="c",Age=20,StuNo="c007"}
};
//添加testStr 元素
IEnumerable<IGrouping<string, Student>> query = list.GroupBy(x =>x.ClassName);
foreach (IGrouping<string, Student> info in query)
{
Console.WriteLine("班级:" + info.Key);
List<Student> sl = info.ToList<Student>();//分组后的集合
foreach (Student item in sl)
{
Console.WriteLine("姓名:" + item.Name + "\t班级:" + item.ClassName + "\t年龄:" + item.Age + "\t学号:" + item.StuNo);
}
}
Console.Read();
}
}
class Student
{
public string Name { get; set; }
public string ClassName { get; set; }
public int Age { get; set; }
public string StuNo { get; set; }
}
List分组操作
最新推荐文章于 2023-09-06 08:42:38 发布