C# list分组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace demo1
{
    class Program
    {


       // static char[] Tranform = new char[36] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
       //static string Tranform10To36Hex(long vl_value)
       // {
       //     int i;
       //     byte[] a = new byte[8];
       //     byte[] b = new byte[8];
       //     string Hex36Str;
       //     for (i = 0; i < 7; i++)
       //     {
       //         if (vl_value < 36)
       //         {
       //             a[i] = Convert.ToByte(vl_value.ToString());
       //             break;
       //         }
       //         a[i] = Convert.ToByte(vl_value % 36);
       //         vl_value = vl_value / 36;
       //     }
       //     for (i = 0; i < 7; i++)
       //     {
       //         b[i] = Convert.ToByte(Tranform[Convert.ToInt32(a[7 - i - 1])]);
       //     }
       //     Hex36Str = System.Text.Encoding.ASCII.GetString(b);
       //     return Hex36Str;
       // }


        static void Main(string[] args)
        {


            //WebReference.CargetoilOvaluesService cgo = new WebReference.CargetoilOvaluesService();


            //decimal dd = cgo.GetOvalue("460020831369683", Convert.ToDecimal(171.50));


            //Console.WriteLine(dd);


            //long aa = 2173500393;
            //string bb=Tranform10To36Hex(aa);
            //Console.WriteLine(bb);


            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"}
           };
            Dictionary<string, List<Student>> stuGroup = new Dictionary<string, List<Student>>();


            foreach (Student item in list)
            {
                if (!stuGroup.Keys.Contains(item.ClassName))
                {
                    stuGroup.Add(item.ClassName, new List<Student>());
                }
                stuGroup[item.ClassName].Add(item);


            }
            foreach (KeyValuePair<string, List<Student>> item in stuGroup)
            {
                Console.WriteLine("班级:" + item.Key);
                foreach (Student stu in item.Value)
                {
                    Console.WriteLine("姓名:" + stu.Name + "\t班级:" + stu.ClassName + "\t年龄:" + stu.Age + "\t学号:" + stu.StuNo);
                }
            }
            Console.Read();




















            Console.Read();
                
        }
    }
    class Student
    {
        public string Name { get; set; }


        public string ClassName { get; set; }


        public int Age { get; set; }


        public string StuNo { get; set; }


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值