第三十四讲 LINQ 基础语法(一)

来源:互联网 发布:哪个剪辑软件最快 编辑:程序博客网 时间:2024/04/30 01:10
这节课老师是通篇的讲实例操作,于是我就看了两遍,靠理解默写老的实例,不过比老师懒,哈哈。不多说了,把代码贴出来吧,有很多不规范的地方,大鸟多提些意见,就我个人知道的最大的问题是变量命名有点小混乱。

还有:以下是后面几个实例 ,前面的实例我双休在家写的,今晚也贴上来。


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Con33{    class Program    {        static void Main(string[] args)        {            List<BanJi> bj = new List<BanJi>() {                 new BanJi(1,"一班"),                new BanJi(2,"二班"),                new BanJi(3,"三班"),                new BanJi(4,"四班"),                new BanJi(5,"五班")            };            List<students> stus = new List<students>() {                 new students(1,"张小三",3),                new students(1,"李小四",2),                new students(1,"王小五",1),                new students(1,"赵小六",1),                new students(1,"麻八",4),                new students(1,"张三",5),                new students(1,"张大三",2),                new students(1,"李三",2),                new students(1,"赵小三",2),                new students(1,"卓小三",3),                new students(1,"小三",4),                new students(1,"小四",5)            };            var stu = from s in stus                      from b in bj                      where s.bjId == b.bjId                      select new { s, b };            foreach (var v in stu)            {                Console.WriteLine(v.s.ToString() + v.b.ToString());            }            Console.WriteLine("\n===========分组=============\n");            var stu2 = from s in stus                       orderby s.bjId                       group s by s.bjId;            foreach (IGrouping<int, students> istu in stu2)            {                Console.WriteLine("\n***************班级ID为{0}**************\n", istu.Key);                foreach (students s in istu)                {                    Console.WriteLine(s.ToString());                }            }            Console.WriteLine("\n===========group by into=============\n");            var stu3 = from s in stus                       orderby s.stuId                       group s by s.bjId into c //将分组后的学生集合存储到组缓存中                       from b in bj                       where b.bjId == c.Key                       select new { bjInfo = string.Format("班级ID={0}, 班级名={1} ", b.bjId, b.bjName), c };            foreach (var v in stu3)            {                Console.WriteLine("\n班级信息是:{0}\n", v.bjInfo);                foreach (students itu in v.c)                {                    Console.WriteLine(itu.ToString());                }            }            Console.WriteLine("\n===========group by into千变万化啊=============\n");            //声明,写着写着,变量写的就不正规了,还请见谅,细细的看            var stu4 = from s in stus                       group s by s.bjId into c                       select new                       {                           bj = string.Format("班级ID={0}", c.Key),                           v = from a in c                               where a.stuName.Contains("张")                               select a                       };            foreach (var va in stu4)            {                Console.WriteLine(va.bj);                foreach (var vb in va.v)                {                    Console.WriteLine(vb.ToString());                }            }            Console.WriteLine("\n===========let子句=============\n");            List<students> listu = new List<students>() {                 new students("二货",new List<int>(){56,78,90}),                new students("三货",new List<int>(){88,78,34}),                new students("一货",new List<int>(){98,23,90}),                new students("四货",new List<int>(){100,65,77}),                new students("五货",new List<int>(){44,78,90}),                new students("六货",new List<int>(){56,66,99})            };            //求他们的平均成绩            var stu5 = from s in listu                       let total = s.score[0] + s.score[1] + s.score[2]                       where total / 3 > 70                       select new {name=s.stuName,score=total/3 };            foreach (var v in stu5)            {                Console.WriteLine("中奖的学生有:{0}, 平均成线是: {1}",v.name,v.score);            }            Console.ReadKey();        }    }    /// <summary>    /// 班级类    /// </summary>    public class BanJi    {        public int bjId;//班级编号        public string bjName;//班级名                public BanJi(int id, string name)        {            this.bjId = id;            this.bjName = name;        }        //重写ToString方法        public override string ToString()        {            return string.Format("所在的班级是:{0}", this.bjName);        }    }    /// <summary>    /// 学生类    /// </summary>    public class students    {        public int stuId;//学生编号        public string stuName;//学生名        public int bjId;//学生所在班级        //学生的语数外成绩单        public List<int> score;        /// <summary>        /// 你懂的,这个构造就是为上面的练习准备的,下同        /// 构造学生名和学生成绩单        /// </summary>        /// <param name="name"></param>        /// <param name="s"></param>        public students(string name, List<int> s)        {            this.stuName = name;            this.score = s;        }        /// <summary>        /// 构造学生的编号,名字及所在的班级        /// </summary>        /// <param name="sid"></param>        /// <param name="name"></param>        /// <param name="cid"></param>        public students(int sid, string name, int cid)        {            this.stuId = sid;            this.stuName = name;            this.bjId = cid;        }        /// <summary>        /// 复写ToString方法        /// </summary>        /// <returns></returns>        public override string ToString()        {            return string.Format("学生信息:ID={0}, 姓名={1},", this.stuId, this.stuName);        }    }}


0 0
原创粉丝点击