认识 Linq

Posted ifordream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了认识 Linq相关的知识,希望对你有一定的参考价值。

            List<Student> Students = new StudentDbProvider().StudentOperation.Get().ToList();
            List<StudentScore> csScores = new StudentDbProvider().StudentScoreOperation.Get("C#").ToList();
            List<StudentScore> dbScores = new StudentDbProvider().StudentScoreOperation.Get("DB").ToList();
            //查询查询课程总分和平均分 ---非Linq方式
            int csScoreSum = 0;
            int dbScoreSum = 0;
            double csScoreAvg = 0.0;
            double dbScoreAvg = 0.0;
            foreach (StudentScore csScore in csScores)
            {
                csScoreSum += Convert.ToInt32(csScore.Score);
            }

            foreach (StudentScore dbScore in dbScores)
            {
                dbScoreSum += Convert.ToInt32(dbScore.Score);
            }
            csScoreAvg = csScoreSum / Students.Count();
            dbScoreAvg = dbScoreSum / Students.Count();

            Console.WriteLine("C#课程分总和:" + csScoreSum + "平均分:" + csScoreAvg);
            Console.WriteLine("DB课程分总和:" + dbScoreSum + "平均分:" + dbScoreAvg);
            Console.ReadLine();

            //查询查询课程总分和平均分 ---Linq方式
            Console.WriteLine("C#课程分总和:{0}平均分:{1}", csScores.Sum(c=>c.Score),csScores.Average(c=>c.Score));
            Console.WriteLine("DB课程分总和:{0}平均分:{1}", dbScores.Sum(c => c.Score), dbScores.Average(c => c.Score));
            Console.ReadLine();

            //根据学生查询各学科的总分和平均分 ---非Linq方式
            List<StudentScoreReport> Reports = new List<StudentScoreReport>();
            foreach (Student student in Students)
            {
                StudentScoreReport report = new StudentScoreReport();
                report.StudentId = student.Id;
                report.Name = student.Name;
                foreach (StudentScore csScore in csScores)
                {
                    if (csScore.Id == student.Id)
                    {
                        report.ScoreSum += csScore.Score;
                        break;
                    }
                }
                foreach (StudentScore dbScore in dbScores)
                {
                    if (dbScore.Id == student.Id)
                    {
                        report.ScoreSum += dbScore.Score;
                        break;
                    }
                }
                report.ScoreAvg = report.ScoreSum / 2;
                Reports.Add(report);
            }

            foreach (StudentScoreReport report in Reports)
            {
                Console.WriteLine("学生:{0} 的总分:{1},平均分:{2}", report.Name, report.ScoreSum, report.ScoreAvg);
            }
            Console.ReadLine();

            //根据学生查询各学科的总分和平均分 ---Linq方式
            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);
            }
            Console.ReadLine();

            //扩展类
            int Money=123456789;
            double p = 0.1029;
            Console.WriteLine(Money.FormatMoney());
            Console.WriteLine(p.FormatPercent());
            Console.ReadLine(); 

  

以上是关于认识 Linq的主要内容,如果未能解决你的问题,请参考以下文章

Linq语句的认识

认识 Linq

并行LINQ PLinq

C#图解教程 第十九章 LINQ

ASP.NET MVC4.0+EF+LINQ+bui+网站+角色权限管理系统

C#数据类型的认识