普通查询

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
};

linq小结的更多相关文章

  1. C#编程(六十八)----------LINQ小结

    LINQ小结 一.LINQ是什么 LINQ也就是Language Interrated Query的缩写,怎么一个缩写法我也不明白,即语言集成查询,是微软在.NET3.5中提出的一项新技术,LINQ主 ...

  2. C—LINQ小结

    LINQ代表语言集成查询(Language-Integrated Query),它包括用于从数据源检索信息的一组功能.数据检索是许多程序的重要组成功能. 简介:System.Linq; var num ...

  3. JS系列——Linq to js使用小结

    前言:前面几篇介绍了下C#基础技术中的几个:反射.特性.泛型.序列化.扩展方法.Linq to Xml等,本来还有两三个知识点没有写完,比如委托.多线程.异步等,后面会陆续将它们补起来,以便作为一套完 ...

  4. Linq知识小结

    Linq语法小结:有两种形式的语法可供我们在写Linq查询时使用,分别是“查询语法”.“方法语法”.1)先看个列子,有个直观认识     int[] arr = { 12, 2,45,34,23,18 ...

  5. LINQ To SQL && Lambda 使用方法小结 (转)

    1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq: from s in Students   ...

  6. Linq to SQL 小结

    前天开始看这方面的资料,虽然看了网上对比 sql和linq的速度,万条数据可能要慢1/4左右的数度,但是介于的方便,还是学了 首先看看linq的基本语法: FROM XX IN DATASOURCE ...

  7. Code First开发系列之管理数据库创建,填充种子数据以及LINQ操作详解

    返回<8天掌握EF的Code First开发>总目录 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LINQ to ...

  8. Linq查询表达式

    目录 1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. ...

  9. sqlite的ef使用小结

    最近有一个小项目,老师推荐我用下sqlite这种轻型的数据库来进行数据的存储.轻型数据库具有其独特之处:方便,不用安装特定的软件就能够实用,关于sqlite的优点我不赘述,网上还是有好多资料的. 但我 ...

  10. 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解

    本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...

随机推荐

  1. Linux 第三节(重定向符,通配符,管道符,转义符,VIM编辑器)

    1.输入重定向符 < 2.输出重定向符 将我们的命令原本要输出到屏幕的内容,输出到文件里面 标准信息 >  覆盖>  追加>> 错误信息 2>  覆盖2>  ...

  2. HTML复习(17.表格样式)

    重点 掌握caption-side(表格标题位置) 掌握border-collapse(表格边框合并) 掌握border-spacing(表格边框间距) 表格标题位置在CSS中,我们可以使用capti ...

  3. 解决href 不下载直接跳转到新的页面

    1.下载 不支持所有浏览器 2 var eleTextarea = document.querySelector('textarea'); var eleButton = document.query ...

  4. Xcode常用&开发常用

    p.p1 { margin: 0; font: 12px "Helvetica Neue" } p.p2 { margin: 0; font: 12px "Helveti ...

  5. 一次讲清promise

    此文章主要讲解核心思想和基本用法,想要了解更多细节全面的使用方式,请阅读官方API 这篇文章假定你具备最基本的异步编程知识,例如知道什么是回调,知道什么是链式调用,同时具备最基本的单词量,例如page ...

  6. simis报错总结

    --笔记开始: 1.在前台模块处理时,[单位应收核定]比[人员缴费信息]的在职人员多一人,但是总金额一样,可能是以下原因造成!!! A.从后台看,若正常核定在职的ab08比ac13多一个人,可能是ac ...

  7. 实验1 Python开发环境使用和编程初体验

    # print输出的几种用法 # 用法1:用于输出单个字符串或单个变量 print('hey, u') # 用法2: 用于输出多个数据项,用逗号分隔 print('hey', ' u') x,y,z ...

  8. BGP反射器

    路由反射器是一种减少自治系统内IBGP对等体连接数量的方法. 根据BGP路由通告原则,要求一个AS内的所有BGP Speaker将建立全连接关系(BGP Speaker两两建立邻接关系).当AS内的B ...

  9. Python爬虫之Scrapy框架爬虫实战

    Python爬虫中Scrapy框架应用非常广泛,经常被人用于属于挖掘.检测以及自动化测试类项目,为啥说Scrapy框架作为半成品我们又该如何利用好呢 ?下面的实战案例值得大家看看. 目录: 1.Scr ...

  10. 狂神学习笔记domo6

    1.新特性,1000000000可以写成10_0000_0000便于阅读 2.强制类型转换 先强制类型转换再赋值才能正确的结果 public class domo06 { public static ...