LINQ

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Linq02_CollectionWithLINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = GetStudents();
            List<StudentScore> csScores = GetCSScores();
            List<StudentScore> dbScores = GetDBScores();

            // 彙總成績
            //Console.WriteLine("C#課程分數加總:{0}, 平均:{1}",
            //    csScores.Sum(c => c.Score), 
            //    csScores.Average(c => c.Score).ToString("0.00"));
            //Console.WriteLine("DB課程分數加總:{0}, 平均:{1}",
            //    dbScores.Sum(c => c.Score), 
            //    dbScores.Average(c => c.Score).ToString("0.00"));

            // 依學生彙總
            var studentScoreQuery =
                from student in students
                join csScore in csScores on student.Id equals csScore.Id
                join dbScore in dbScores on student.Id equals dbScore.Id
                select new
                {
                    Id = student.Id,
                    Name = student.Name,
                    ScoreSum = csScore.Score + dbScore.Score,
                    ScoreAvg = (csScore.Score + dbScore.Score) / 2
                };


            foreach (var studentScore in studentScoreQuery)
            {
                Console.WriteLine("學生 {0} 分數加總:{1}, 平均:{2}",
                    studentScore.Name, 
                    studentScore.ScoreSum, 
                    studentScore.ScoreAvg.ToString("0.00"));
            }

            Console.ReadLine();
        }

        private static List<Student> GetStudents()
        {
            return new List<Student>(new[] 
            {
                new Student() {
                  Id = "001", Name = "張三"
                },
                new Student() {
                  Id = "002", Name = "李四"
                },
                new Student() {
                  Id = "003", Name = "王五"
                },
                new Student() {
                  Id = "004", Name = "陳六"
                },
                new Student() {
                  Id = "005", Name = "飛七"
                }
            });
        }

        private static List<StudentScore> GetCSScores()
        {
            return new List<StudentScore>(new[] 
            {
                new StudentScore() {
                  Id = "001", Score = 82
                },
                new StudentScore() {
                  Id = "002", Score = 65
                },
                new StudentScore() {
                  Id = "003", Score = 43
                },
                new StudentScore() {
                  Id = "004", Score = 78
                },
                new StudentScore() {
                  Id = "005", Score = 90
                }
            });
        }

        private static List<StudentScore> GetDBScores()
        {
            return new List<StudentScore>(new[] 
            {
                new StudentScore() {
                  Id = "001", Score = 55
                },
                new StudentScore() {
                  Id = "002", Score = 61
                },
                new StudentScore() {
                  Id = "003", Score = 72
                },
                new StudentScore() {
                  Id = "004", Score = 90
                },
                new StudentScore() {
                  Id = "005", Score = 82
                }
            });
        }
    }

    public class Student
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

    public class StudentScore
    {
        public string Id { get; set; }
        public int Score { get; set; }
    }
}
复制代码

 

posted @   超难微猫  阅读(80)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示