linq小结
普通查询
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小结的更多相关文章
- C#编程(六十八)----------LINQ小结
LINQ小结 一.LINQ是什么 LINQ也就是Language Interrated Query的缩写,怎么一个缩写法我也不明白,即语言集成查询,是微软在.NET3.5中提出的一项新技术,LINQ主 ...
- C—LINQ小结
LINQ代表语言集成查询(Language-Integrated Query),它包括用于从数据源检索信息的一组功能.数据检索是许多程序的重要组成功能. 简介:System.Linq; var num ...
- JS系列——Linq to js使用小结
前言:前面几篇介绍了下C#基础技术中的几个:反射.特性.泛型.序列化.扩展方法.Linq to Xml等,本来还有两三个知识点没有写完,比如委托.多线程.异步等,后面会陆续将它们补起来,以便作为一套完 ...
- Linq知识小结
Linq语法小结:有两种形式的语法可供我们在写Linq查询时使用,分别是“查询语法”.“方法语法”.1)先看个列子,有个直观认识 int[] arr = { 12, 2,45,34,23,18 ...
- LINQ To SQL && Lambda 使用方法小结 (转)
1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq: from s in Students ...
- Linq to SQL 小结
前天开始看这方面的资料,虽然看了网上对比 sql和linq的速度,万条数据可能要慢1/4左右的数度,但是介于的方便,还是学了 首先看看linq的基本语法: FROM XX IN DATASOURCE ...
- Code First开发系列之管理数据库创建,填充种子数据以及LINQ操作详解
返回<8天掌握EF的Code First开发>总目录 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LINQ to ...
- Linq查询表达式
目录 1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. ...
- sqlite的ef使用小结
最近有一个小项目,老师推荐我用下sqlite这种轻型的数据库来进行数据的存储.轻型数据库具有其独特之处:方便,不用安装特定的软件就能够实用,关于sqlite的优点我不赘述,网上还是有好多资料的. 但我 ...
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...
随机推荐
- CSS中z-index的属性与使用
z-index 属性指定一个元素的堆叠顺序. 拥有更高堆叠顺序的元素总是会处于堆叠顺序较低的元素的前面. Z-index 仅能在定位元素上奏效,z-index 进行定位元素(position:abso ...
- SQL-建表注释
ddl 是对表结构的操作 create(创建)命令.alter(修改)命令.drop(删除)dml 是对表数据的操作 insert(插入)命令.update(更新)命令.delete(删除) alte ...
- 杭电OJ--1003题C++实现
#include<iostream>using namespace std;int a[100000];void solve(int k,int n,int t);int main(){ ...
- Navicat12安装包+破解方式(详细教程)
链接:https://pan.baidu.com/s/1vXQzT5nWD73lS5ZMGYYfeA 提取码:phhh 注意!!! 只有Navicat12版本才支持破解,其他版本无法破解. 1. 下 ...
- C++ 复习函数的基本知识
C++ 复习函数的基本知识 要使用 C++ 函数,必须完成如下工作: 1. 提供函数定义: 2. 提供函数原型: 3. 调用函数. 例子: #include <iostream> usin ...
- vue 数组对象去重
unique(arr) { const res = new Map(); return arr.filter((arr) => !res.has(arr.id) &&am ...
- 图论之最小生成树问题(kruskal)
最近有几位同学催我更新,于是来摸摸鱼,来讲一下最小生成树问题. 所谓最小生成树(MST),就是在一张无向带权图中的一棵经过所有节点,边权和最小的一棵树.在实际生活中,可以运用于城镇之间的修路上. 对于 ...
- SpringBoot系列---【maven项目引入第三方jar包并打包发布】
一.问题 项目中经常会碰到这样的问题,我们做的项目依赖别人打好的jar包,这种我们可以有两种途径解决,第一种是上传到私服,再从我们的项目去引入pom坐标,这种适合有私服账号或者自己会搭建私服的,成本有 ...
- Qt打印不同颜色
qCritical()<<"\033[47;31m"<<"打印的字符串"<<"\033[m"; \033 ...
- 十大经典排序之快速排序(C++实现)
快速排序 通过一趟排序将待排序列分割成两部分,其中一部分记录的关键字均比另一部分记录的关键字小.之后分别对这两部分记录继续进行排序,以达到整个序列有序的目的. 思路: (1)选择基准:从数列中挑出一个 ...