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;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值