Linq无聊练习系列6--Any/All/Contains/Concat/U

系统 1474 0

/*********************Any/All/Contains/Concat/Union/Intersect/Except/take/skip/SqlMethods操作练习**********************************/
            //判断没有成绩的学生
            var list = from s in ctx.T_Student
                       where !s.T_Score.Any()
                       select s;
            //判断有成绩大于80的学生
            var list1 = (from s in ctx.T_Student
                         where s.T_Score.Any(t => t.score > 80)
                         select s).Distinct();
            //所有科目成绩大于80的学生
            var list2 = from s in ctx.T_Student
                        where s.T_Score.All(t=>t.score>80)
                        select s;
            //查询黄阳是否参加过考试
            string[] names = { "黄阳"};
            var list4 = from s in ctx.T_Score
                        where names.Contains(s.T_Student.stuName)
                        select s;
            //查询黄阳是否参加过考试
            var list3 = from s in ctx.T_Score
                        where (new string[] { "黄阳" }).Contains(s.T_Student.stuName)
                        select s;
            //参加过考试但是不是黄阳的同学
            var list5 = from s in ctx.T_Score
                        where !(new string[] { "黄阳" }).Contains(s.T_Student.stuName)
                        select s;
            //contains也可以包含一个对象,如查询那些学生参加了课程编号为001的第一个同学,对于contains只能包含一个实体,不能保护实体集
            var temp1 = (from s in ctx.T_Score
                        where s.stuNumber == "001"
                        select s).First();
            var result = ctx.T_Student.Where(p=>p.T_Score.Contains(temp1));
            //查找黄阳和黄波的所有成绩
            string[] names1 = {"黄阳","黄波"};
            var result2 = ctx.T_Score.Where(f=>names1.Contains(f.T_Student.stuName));
            //或者也可以这样
            var result3 = from s in ctx.T_Score
                          where (new string[] { "黄阳", "黄波" }).Contains(s.T_Student.stuName)
                          select s;
            //对于concat,连接不同的集合,不会自动过滤相同项,而且它只能对于单列进行连接,对多列进行连接,会报语法错误,因为没有相应的扩展方法

            var result4 = (from s in ctx.T_Student
                           select s.stuName)
                          .Concat(from k in ctx.T_Score
                                  select k.T_Cource.courceName );
            //合并所有的学生
            var result5 = ((from s in ctx.T_Student
                            select s.stuNumber)
                        .Union
                        (from k in ctx.T_Score
                         select k.stuNumber)).Distinct();
            //交集
            var result6 = (from s in ctx.T_Student
                           select s.stuNumber).Intersect
                          (from k in ctx.T_Score
                               select k.stuNumber);
            //差集
            var result7 = (from s in ctx.T_Student
                           select s.stuNumber).Intersect
                               (from k in ctx.T_Score
                                select k.stuNumber);
            //查询前三个学生的信息
            var result8 = (from s in ctx.T_Student
                          select s).Take(3);
            //查询三个学生以外的所有学生信息
            var result9 = (from s in ctx.T_Student
                               select s).Skip(3);
            //当学号为2091723时候,就停止获取学生信息
            var result10 = (from s in ctx.T_Student
                            select s).TakeWhile(k => k.stuNumber == "2091723");
            //当学号为2091723时候,就停止跳过学生信息,并获取后边的所有学生信息
            var result11 = (from s in ctx.T_Student
                            select s).SkipWhile(k => k.stuNumber == "2091723");
            //可以用skip和take来对数据进行分页,这非常方便

            //查看姓名以黄开头的学生信息,这时候程序需要引入using System.Data.Linq.SqlClient;命名空间
            var result12 = from s in ctx.T_Student
                           where SqlMethods.Like(s.stuName,"黄%")
                           select s;
            //查询学号为2X917X2的学生
            var result13 = from s in ctx.T_Student
                           where SqlMethods.Like(s.stuNumber, "2_917_2")
                           select s;
            //比较时间的操作分别有:DateDiffDay、DateDiffHour、DateDiffMillisecond、DateDiffMinute、DateDiffMonth、DateDiffSecond、DateDiffYear ,也是sqlMethods方法,而数据库中的表并没有设计时间的字段,所以就不演示了


            //SQL语句进行编辑重新查询,需要引用命名空间using System.Data.Linq;
            var fn = CompiledQuery.Compile(
                (DB_StudentDataContext ctx1,string stuName)=>
                from s in ctx1.T_Student
                where s.stuName==stuName
                select s
                );
            var data1 = fn(ctx,"黄阳");
            var data2 = fn(ctx,"黄波");

Linq无聊练习系列6--Any/All/Contains/Concat/Union/Intersect/Except/take/skip/SqlMethods操作练习


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论