Linq不分组统计、子查询、计算列及索引器的应用

using System;
using System.Linq;

namespace e2
{
    class Program
    {
        static void Main(string[] args)
        {
            Student[] Class1 = {
                new Student(11,"张三",new float[]{55,91,77}),
                new Student(19,"李四",new float[]{43.5F,59,80}),
                new Student(33,"王五",new float[]{80,95,90})};

            //平均成绩,!!!分组
            var avgScore = from s in Class1
                           group s by 1 into g
                           select new
                           {
                               am = g.Average(x => x.Score[0]),
                               ae = g.Average(x => x.Score[1]),
                               ac = g.Average(x => x.Score[2])
                           };
            foreach (var i in avgScore)
            {
                Console.WriteLine("数学平均成绩:{0:#.##}", i.am);
                Console.WriteLine("英语平均成绩:{0:#.##}", i.ae);
                Console.WriteLine("语文平均成绩:{0:#.##}", i.ac);
            }
            //2门及以上不合格的学生,不合格的课程,!!!子查询
            Console.WriteLine("\n2门以上不合格的学生及其不合格的课程:\n");
            var noPass = from s in Class1
                         where (from f in s.Score where f < 60 select f).Count() > 1
                         select s;
            foreach (var s in noPass)
            {
                Console.Write("学号:{0}\t姓名:{1}", s.ID, s.Name);
                if (s.Score[0] < 60) Console.Write("\t数学:{0:#.#}", s["数学"]);
                if (s.Score[1] < 60) Console.Write("\t英语:{0:#.#}", s[1]);
                if (s.Score[2] < 60) Console.Write("\t语文:{0:#.#}", s.Score[2]);
                Console.WriteLine();
            }
            //三门课平均成绩在85-90分的学生,!!!计算列及投影
            Console.WriteLine("\n三门课平均成绩在85-90分的学生:\n");
            var GoodStudent = from s in Class1
                              let AvgScore = s.Score.Average()
                              where AvgScore >= 85 && AvgScore <= 90
                              select new { s.ID, s.Name, AvgScore };
            foreach (var s in GoodStudent)
            {
                Console.Write("学号:{0}\t姓名:{1}\t平均分:{2:#.##}", s.ID, s.Name, s.AvgScore);
            }
            Console.WriteLine();
            Console.ReadKey();
        }
    }
    public class Student
    {
        public int ID { get; private set; }
        public string Name { get; private set; }
        public float[] Score { get; private set; }
        public Student(int cID, string cName, float[] cScore) { ID = cID; Name = cName; Score = cScore; }
        public float this[string km]
        {
            get
            {
                switch (km)
                {
                    case "数学": return Score[0];
                    case "英语": return Score[1];
                    case "语文": return Score[2];
                }
                return 0;
            }

        }
        public float this[int i]
        {
            get
            {
                if (i >= 0 && i <= 2) return Score[i];
                return 0;
            }
        }
    }
}

你可能感兴趣的:(C#)