首页 > 其他分享 >linq小结

linq小结

时间:2022-09-03 22:33:18浏览次数:61  
标签: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
From: https://www.cnblogs.com/SYF--BLOG/p/16653849.html

相关文章

  • 一篇文章教你学会ASP.Net Core LINQ基本操作
    一篇文章教你学会ASP.NetCoreLINQ基本操作为什么要使用LINQLINQ中提供了很多集合的扩展方法,配合lambda能简化数据处理。例如我们想要找出一个IEnumerable<int>中所有......
  • CH9121小结
    当通过网络配置工具将CH9121设置成UDPSERVER模式时,同时通过SRT-NET设置将PC设置成UDP并与CH9121用网线连接。将CH9121配置好与PC信息相匹配的端口号与IP地址,PC端相应......
  • C#并行编程:PLINQ
    PLINQ可以自动并行化本地LINQ查询。易于使用是PLINQ的优势,因为它将工作划分和结果整理的任务交给了.NETCore。要使用PLINQ,只需直接在输入序列上调用AsParallel()方法,而后......
  • Millar-Rabin 米勒罗宾算法小结 (内附费马小定理证明以及二次探测定理证明)
    因为他我学了龟速乘Millar-robin米勒罗宾这个小东西是用来素数判定的,且听我细细道来。前置知识肥妈小定理又名费马小定理:当一个数\(x\)不是一个质数\(p\)的......
  • hadoop小结
    Hadoop是一个适合海量数据的分布式存储和分布式计算的平台主要有以下功能:HadoopCommon:基础型功能HadoopDistributedFileSystem(HDFS™):一种分布式文件系统,可提供对......
  • LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria
    LINQ:WhentouseSingleOrDefaultvs.FirstOrDefault()withfilteringcriteria问题   回答1Ifyourresultsetreturns0records:SingleOrDefaultretu......
  • 代码圈复杂度治理小结
    简介: 我们一直在说系统很复杂,那到底什么是系统复杂度呢?作为团队的稳定性底盘负责人,也经常和大家探讨为什么会因为圈复杂度高而被扣分。那么,怎么才能写的一手可读,可扩展,可......
  • c# XElement linq filter
    usingSystem.Xml;usingSystem.Xml.Linq;//调用staticprivatevoidInitIncrementalSanctionsReferencesList(thisXmlReaderreader,PFApfa)......
  • Linq SequenceEqual
    publicclassUnitTest1{publicclassPerson{publicstringName{get;set;}publicintAge{get;set;}......
  • Linq:Distinct()不能排除重复对象的解决方案
    1.数据准备:假设有几个重复数据,如下,(正常使用Distinct()方法,我们想要排除掉重复的对象)usingSystem.Collections.Generic;namespaceLINQTutorial{publicclassS......