普通查询

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. VS2012下没有ADO.NET实体数据模型

    在C盘下搜"EFTools.msi"然后退出VS,点击修复在打开VS,数据下就有了

  2. 教你三分钟开发开发java短信验证码

    现如今,绝大多数网站和app都需要支持手机号注册.手机登录,这就需要开发者实现短信验证码的功能,对于很多小白同学来说,没接触过,没有思路,下面小编就给大家详解一下. 发送短信的功能需要借助第三方的短信 ...

  3. hive:使用concat_ws实现 array转string案例

    concat_ws(',',collect_set(if(step_name <> '',step_name,null))) AS step_names,

  4. Ubuntu ROOT默认密码设置

    Ubuntu默认密码设置 Ubuntu默认是不设置ROOT密码的,如果需要安装一些包则需要从普通用户调转至ROOT用户,这个时候就需要设置一下密码了. 下面这种情况 chen@chen:~/Deskt ...

  5. JS斐波那契数列O(n)

    function fibonacci(n) { return fib(n)[n] } var fib=(function(n){ var meo=[0,1] return function(n){ f ...

  6. Java中File类

    File类是java.io包中唯一代表磁盘文件本身的对象.File类的对象主要用来获取文件本身的一些信息,如文件所在目录.文件长度.读写权限等. 一. 文件的创建与删除 通常使用以下三种方法来创建一个 ...

  7. AX2012 去掉浮点数后面的0

    static void Job116(Args _args) { str string1; real num1; ; num1 = 0.00; string1 = System.String::For ...

  8. 会议室NTP同步时钟布置与系统建设要领

    深圳市立显电子有限公司,专业LED时钟生产厂家!--------[点击进入]  NTP同步时钟之会议室设计要领:   会议室是企业必不可少的办公配套用房,一般分为大中小不同类型,有的企业中小会议室有多 ...

  9. idea创建maven + tomcat项目

    移动文件夹: 找到tomcat文件目录 配置完成,添加测试html文件: 运行 修改默认路径:

  10. Windows安装使用Chocolatey 包软件管理(类似 rpm , yum, brew , apt-get 包管理器工具)

    Windows也能像Linux或者Mac那样命令行安装管理软件了,,,真的太方便了 下载安装 使用window powershell 用管理员运行 Set-ExecutionPolicy Bypass ...