ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

linq小结

2022-09-03 22:30:20  阅读:265  来源: 互联网

标签:Student linq context var query 小结 Id select


普通查询

var query = from s in context.Student select s;

//投影列
var query = from s in context.Student select new { s.Id, s.StudentName };

//起别名
var query = from a in context.Student select new { 姓名 = a.StudentName, 性别 = a.Sex };

排序

                //单字段排序
                var query = from s in context.Student orderby s.Id ascending select s;
                // 多字段排序
                var query2 = (from s in context.Student
                              orderby s.Id ascending, s.StudentName descending
                              select s);// descending:降序

分组

                var query = from s in context.Student group s by s.ClassId;  //该行会报错'Client side GroupBy is not supported.',需要先取出数据再进行分组
                var query = context.Student.ToList().GroupBy(x => x.ClassId);
                foreach (var item in query)
                {
                    foreach (var data in item)
                    {
                        Console.WriteLine(data.StudentName + "-----" + item.Key);

                    }
                }
//-----------------------------------------------------------------------------------------------------------------------------
                var query = from s in context.Student
                            group s by s.ClassId into gs
                            select new
                            {
                                classId = gs.Key, // ClassId: 分组的键,
                                count = gs.Count()
                            };
                foreach (var g in query)
                {
                    Console.WriteLine($"班级:{g.classId},数量:{g.count}");
                }

模糊查询

                //模糊查询
                // like '%任%'
                var query = from s in context.Student where s.StudentName.Contains("任") orderby s.Id ascending select s;
                // like '任%'
                var query2 = from s in context.Student where s.StudentName.StartsWith("任") orderby s.Id ascending select s;
                // like '%任'
                var query3 = from s in context.Student where s.StudentName.EndsWith("任") orderby s.Id ascending select s;

分页

                //分页 skip((pageIndex-1)*pageSize).Take(pageSize)
                var query = from s in context.Student.Skip((1 - 1) * 3).Take(3) select s;

连接查询

                //内连接

                //select 
                //Student.StudentName,Student.Birthday,Student.Sex,Score.Course,Score.Degree
                //from Student
                //inner join Score
                //on Student.Id = Score.StudentId

                var query = from s in context.Student
                            join sc in context.Score
                            on s.Id equals sc.StudentId
                            select
                            new
                            {
                                s.Id,
                                s.StudentName,
                                s.Birthday,
                                s.Sex,
                                s.ClassId,
                                sc.Course,
                                sc.Degree
                            };


                //左连接
                var query2 = from s in context.Student
                             join sc in context.Score on s.Id equals sc.StudentId into temp
                             from t in temp.DefaultIfEmpty()
                             select new
                             {
                                 s.Id,
                                 s.StudentName,
                                 s.Birthday,
                                 s.Sex,
                                 s.ClassId,
                                 t.Course,
                                 t.Degree
                             };

标签:Student,linq,context,var,query,小结,Id,select
来源: https://www.cnblogs.com/SYF--BLOG/p/16653849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有