SQL对照LinQ的基本语句
查询全表:
string sql = "SELECT * FROM UserInfo";
var lamAll = db.UserInfo;
var LinAll = from u in db.UserInfo select u;
条件查询
string sql2 = "SELECT * FROM UserInfo where UserId = 1";
var lamWhere = db.UserInfo.Where(u => u.UserId == );
var linWhere = from u in db.UserInfo where u.UserId == select u;
Order by查询
string sql3 = "SELECT * FROM UserInfo where order by UserId desc";
var lamOrderby = db.UserInfo.OrderBy(u => u.UserId);
var linOrderby = from u in db.UserInfo orderby u.UserId descending select u;
带条件Order by
string sql4 = "SELECT * FROM UserInfo where UserId > 10 order by UserId desc/asc";
var lamOrderWhere = db.UserInfo.OrderByDescending(u => u.UserId).Where(u => u.UserId == );
var linOrderWhere = from u in db.UserInfo where u.UserId > orderby u.UserId descending select u;
TOP语句
string sql5 = "SELECT TOP 10 * FROM UserInfo";
var lamTop = db.UserInfo.Take();
var linTop = (from u in db.UserInfo select u).Take(); //Take 从序列的开头返回指定元素
Like语句
string sql5 = "SELECT * FROM UserInfo where Name like '%张%'";
var lamLike = db.UserInfo.Where(u => u.Name.Contains("张"));
var linLike = from u in db.UserInfo where u.Name.Contains("王") select u;
表联合
string ss7 = "SELECT u.*,l.* FROM UserInfo as u inner join dbo.Logs as l on u.UserId = l.UserId";
var l7 = db.UserInfo.Join(db.Logs, u => u.UserId, l => l.UserId, (u, l) => new UserInfoJoinLogs { UserId = u.UserId, Name = u.Name, LName = l.LName }); //UserInfoJoinLogs为一个类,里边存放两表的数据
var s7 = from u in db.UserInfo join l in db.Logs on u.UserId equals l.UserId select new UserInfoJoinLogs { UserId = u.UserId, Name = u.Name, LName = l.LName };
in/not in语句
string UserId = "1,2,3,4";
int[] UserIdArr = { , , , };
string ss8 = "SELECT * FROM UserInfo where UserId in(" + UserId + ")";
string sss8 = "SELECT * FROM UserInfo where UserId not in(" + UserId + ")";
var s8 = from u in db.UserInfo where (UserIdArr).Contains(u.UserId) select u;
简单函数的计算
string max = "SELECT MAX(UserId) FROM UserInfo";
string min = "SELECT min(UserId) FROM UserInfo";
string count = "SELECT COUNT(UserId) FROM UserInfo";
string sum = "SELECT SUM(UserId) FROM UserInfo"; var lmax = db.UserInfo.Max(u => u.UserId);
var lmin = db.UserInfo.Min(u => u.UserId);
var lcount = db.UserInfo.Count();
var lsum = db.UserInfo.Sum(u => u.UserId); var lqmax = (from u in db.UserInfo select u).Max(u => u.UserId);
var lqmin= (from u in db.UserInfo select u).Min(u => u.UserId);
var lqcount = (from u in db.UserInfo select u).Count();
var lqsum = (from u in db.UserInfo select u).Sum(u => u.UserId);
跳过多少条数据查询剩余数据
string ss9 = "SELECT * FROM (SELECT ROW_NUMBER()over(order by UserId desc) as rowNum, * from [UserInfo]) as t where UserId>5";
var l9 = db.UserInfo.OrderByDescending(u => u.UserId).Skip();
var s9 = (from u in db.UserInfo orderby u.UserId descending select u).Skip(); //skip 跳过序列中指定的元素,返回剩余元素
And语句 Or语句
string andsql = "SELECT * FROM UserInfo where UserId=1 and Name='张三'";
var andLab = db.UserInfo.Where(u => ((u.UserId == ) && (u.Name == "张三")));
var orLab= db.UserInfo.Where(u => ((u.UserId == ) || (u.Name == "张三")));
var andlinq = from u in db.UserInfo where u.UserId == && u.Name == "张三" select u;
var orLinq = from u in db.UserInfo where u.UserId == || u.Name == "张三" select u;
区间查询
string sectionSql = "SELECT * FROM UserInfo where UserId >=50 and UserId <=60 ";
var sectionLab = db.UserInfo.Where(e => (((Int32)(e.UserId) >= ) && ((Int32)(e.UserId) <= )));
var sectionLinq = from u in db.UserInfo where u.UserId >= && u.UserId <= select u;
模糊查询加截取指定内容查询
string ss11 = "SELECT e.* FROM UserInfo AS e WHERE e.Name LIKE '张%' OR SUBSTRING(e.Name, 0, 2) = '张三'";
var sLab = db.UserInfo.Where(e => (e.Name.StartsWith("张") || (e.Name.Substring(, ) == "张")));
var s11 = from u in db.UserInfo where u.Name.StartsWith("张") || u.Name.Substring(, ) == "张三" select u;
SQL对照LinQ的基本语句的更多相关文章
- 浅谈sql 、linq、lambda 查询语句的区别
浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...
- Linq Mysql GroupBy语句的问题处理
语句如下: var resumeList = db.ChannelResume.Where(model); var groupValues = resumeList.GroupBy(t => n ...
- 从sql走向linq的我撞死在起点上
[本文纯个人理解,错误轻喷,非常希望能有大神指点] A left (outer) join B on A.bid=B.id 上面这句话叫做左连接,原因是left(左)join(加入,连入)被译为左连接 ...
- Linq To SQL和Linq To Object的批量操作InsertAllOnSubmit介绍
无论是Linq To SQL还是Linq To Object(Entity frameworks)它们都为开发人员提供了Insert操作,及Insert集合操作,即InsertOnSubmit和Ins ...
- PL/SQL客户端中执行insert语句,插入中文乱码
问题描述:在PL/SQL客户端中执行insert语句,插入中文乱码 解决方案: 1.执行脚本 select userenv('language') from dual; 结果为AMERICAN_ ...
- SQL、Linq、lamda表达式 同一功能不同写法
一.SQL.Linq.lamda表达式 同一功能不同写法 SQL LINQ Lambda SELECT * FROM HumanResources.Employee from e in Employe ...
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...
- SQL、LINQ、Lambda 三种用法(转)
SQL.LINQ.Lambda 三种用法颜色注释: SQL LinqToSql Lambda QA1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname, ...
- T-SQL:SQL Server-数据库查询语句基本查询
ylbtech-SQL Server-Basic:SQL Server-数据库查询语句基本查询 SQL Server 数据库查询语句基本查询. 1,数据库查询语句基本查询 数据库 SQL Serv ...
随机推荐
- 解决Linux(Loaded plugins: fastestmirror Please use /usr/bin/yum --help)
大概意思是fastestmirror不能使用,fastestmirror是yum的一个加速插件 处理办法就是禁用这个插件 方法两种 第一种 vi /etc/yum/pluginconf.d/faste ...
- uni-app hbuilderX ios离线打包,启动图修改没反应
最后还是没反应 删除app从新安装, 可参考https://www.jianshu.com/p/47c1377c61d6
- Python编码规范(PEP8)
Introduction 介绍 本文提供的Python代码编码规范基于Python主要发行版本的标准库.Python的C语言实现的C代码规范请查看相应的PEP指南1. 这篇文档以及PEP 257(文档 ...
- 爬虫-requests
一.爬虫系列之第1章-requests模块 爬虫简介 概述 近年来,随着网络应用的逐渐扩展和深入,如何高效的获取网上数据成为了无数公司和个人的追求,在大数据时代,谁掌握了更多的数据,谁就可以获得更高的 ...
- Emacs Org-mode 1 下载、安装、基本使用
1.1 总述 Org 是一种帮助我们做笔记.日常事件或者项目计划的快速高效的文本格式系统. Org 有以下特点: Org mode 基于组织结构(outline-mode)对文本进行组织.具有良好的快 ...
- docker 安装mysql数据库 <二>
一.下载mysql数据库 #网易镜像中心https://c.163.com/hub#/m/home/ #采用网易加速地址,不加速时下载非常的慢 docker pull hub.c..com/libra ...
- python 爬虫与数据可视化--数据提取与存储
一.爬虫的定义.爬虫的分类(通用爬虫.聚焦爬虫).爬虫应用场景.爬虫工作原理(最后会发一个完整爬虫代码) 二.http.https的介绍.url的形式.请求方法.响应状态码 url的形式: 请求头: ...
- Hibernate Session对象核心方法
1. 持久化对象的状态: 站在持久化的角度,Hibernate 把对象分为四种状态:持久化状态,临时状态,游离状态,删除状态 Session 的特定方法能使对象从一个状态转到另一个状态 临时对象: 在 ...
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- 【最短路+最大流】上学路线@安徽OI2006
目录 [最短路+最大流]上学路线@安徽OI2006 PROBLEM SOLUTION CODE [最短路+最大流]上学路线@安徽OI2006 PROBLEM 洛谷P4300 SOLUTION 先在原图 ...