Entity Framework查询
Entity Framework是个好东西,虽然没有Hibernate功能强大,但使用更简便。今天整理一下常见SQL如何用EF来表达,Func形式和Linq形式都会列出来(本人更喜欢Func形式)。
1、简单查询:
SQL:
SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID
EF:
//Func形式
var clients = ctx.Clients.Where(c => c.Type == 1 && c.Deleted == 0)
.OrderBy(c => c.ID)
.ToList(); //Linq形式
var clients = from c in ctx.Clients
where c.Type == 1 && c.Deleted==0
orderby c.ID
select c;
2、查询部分字段:
SQL:
SELECT ID,Name FROM [Clients] WHERE Status=1
EF:
//Func形式
var clients = ctx.Clients.Where(c => c.Status == )
.Select(c => new { c.ID, Name = c.ComputerName })
.ToList(); //Linq形式
var clients = from c in ctx.Clients
where c.Status ==
select new { c.ID, Name = c.ComputerName }; :
3、查询单一记录:
SQL:
SELECT * FROM [Clients] WHERE ID=100
EF:
//Func形式
var client = ctx.Clients.FirstOrDefault(c => c.ID == ); //Linq形式
var client = (from c in ctx.Clients
where c.ID =
select c).FirstOrDefault();
4、LEFT JOIN 连接查询
SQL:
SELECT c.ID,c.ComputerName,g.Name GroupName
FROM [Clients] c
LEFT JOIN [Groups] g
ON c.GroupID = g.ID
WHERE c.Status=1
EF:
//Func形式
var clients = ctx.Clients.Where(c => c.Status == )
.Select(c => new
{
c.ID,
c.ComputerName,
GroupName = ctx.Groups.FirstOrDefault(g => g.ID == c.GroupID).Name
})
.ToList(); //Linq形式
var clients = from c in ctx.Clients
where c.Status ==
select new
{
c.ID,
c.ComputerName,
GroupName = (from g in ctx.Groups
where g.ID == c.GroupID
select g.Name).FirstOrDefault()
};
5、INNER JOIN 连接查询:
SQL:
SELECT c.ID,c.ComputerName,g.Name GroupName
FROM [Clients] c
INNER JOIN [Groups] g
ON c.GroupID = g.ID
WHERE c.Status=1
ORDER BY g.Name
EF:
/Func形式
var clients = ctx.Clients.Where(c => c.Status == )
.Join(ctx.Group, c => c.GroupID, g => g.ID, (c,g) =>
{
c.ID,
c.ComputerName,
GroupName = g.Name
})
.OrderBy(item => item.GroupName)
.ToList(); //Linq形式1
var clients = from c in ctx.Clients
from g in ctx.Groups
where c.GroupID == g.ID
orderby g.Name
select new
{
c.ID,
c.ComputerName,
GroupName = g.Name
}; //Linq形式2
var clients = from c in ctx.Clients
where c.Status ==
join g in ctx.Group
on c.GroupID equals g.ID into result
from r in result
order by r.Name
select new
{
c.ID,
c.ComputerName,
GroupName = r.Name
};
6、分页
SQL:
-- 方案1
SELECT TOP 10 * FROM [Clients] WHERE Status=1
AND ID NOT IN
(
SELECT TOP 20 ID FROM [Clients] WHERE Status=1
ORDER BY ComputerName
)
ORDER BY ComputerName --方案2
SELECT * FROM
(
SELECT *, ROW_NUMBER() OVER(ORDER BY ComputerName) AS RowNo
FROM [Clients]
WHERE Status=1
)t
WHERE RowNo >= 20 AND RowNo < 30
EF:
//Func形式
var clients = ctx.Clients.Where(c => c.Status=)
.OrderBy(c => c.ComputerName)
.Skip()
.Take()
.ToList(); //Linq形式
var clients = (from c in ctx.Clients
orderby c.ComputerName
select c).Skip().Take();
7、分组统计:
SQL:
SELECT Status,COUNT(*) AS Cnt FROM [Clients]
GROUP BY Status
ORDER BY COUNT(*) DESC
EF:
//Func形式
var result = ctx.Clients.GroupBy(c => c.Status)
.Select(s => new
{
Status = s.Key,
Cnt = s.Count()
})
.OrderByDescending(r => r.Cnt); //Linq形式
var result = from c in ctx.Clients
group c by c.Status into r
orderby r.Count() descending
select new
{
Status = r.Key,
Cnt = r.Count()
};
1、简单查询:
SQL:
- SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID
EF:
- //Func形式
- var clients = ctx.Clients.Where(c => c.Type == 1 && c.Deleted == 0)
- .OrderBy(c => c.ID)
- .ToList();
- //Linq形式
- var clients = from c in ctx.Clients
- where c.Type == 1 && c.Deleted==0
- orderby c.ID
- select c;
2、查询部分字段:
SQL:
- SELECT ID,Name FROM [Clients] WHERE Status=1
EF:
- //Func形式
- var clients = ctx.Clients.Where(c => c.Status == 1)
- .Select(c => new { c.ID, Name = c.ComputerName })
- .ToList();
- //Linq形式
- var clients = from c in ctx.Clients
- where c.Status == 1
- select new { c.ID, Name = c.ComputerName }; :
3、查询单一记录:
SQL:
- SELECT * FROM [Clients] WHERE ID=100
EF:
- //Func形式
- var client = ctx.Clients.FirstOrDefault(c => c.ID == 100);
- //Linq形式
- var client = (from c in ctx.Clients
- where c.ID = 100
- select c).FirstOrDefault();
4、LEFT JOIN 连接查询
SQL:
- SELECT c.ID,c.ComputerName,g.Name GroupName
- FROM [Clients] c
- LEFT JOIN [Groups] g
- ON c.GroupID = g.ID
- WHERE c.Status=1
EF:
- //Func形式
- var clients = ctx.Clients.Where(c => c.Status == 1)
- .Select(c => new
- {
- c.ID,
- c.ComputerName,
- GroupName = ctx.Groups.FirstOrDefault(g => g.ID == c.GroupID).Name
- })
- .ToList();
- //Linq形式
- var clients = from c in ctx.Clients
- where c.Status == 1
- select new
- {
- c.ID,
- c.ComputerName,
- GroupName = (from g in ctx.Groups
- where g.ID == c.GroupID
- select g.Name).FirstOrDefault()
- };
5、INNER JOIN 连接查询:
SQL:
- SELECT c.ID,c.ComputerName,g.Name GroupName
- FROM [Clients] c
- INNER JOIN [Groups] g
- ON c.GroupID = g.ID
- WHERE c.Status=1
- ORDER BY g.Name
EF:
- //Func形式
- var clients = ctx.Clients.Where(c => c.Status == 1)
- .Join(ctx.Group, c => c.GroupID, g => g.ID, (c,g) =>
- {
- c.ID,
- c.ComputerName,
- GroupName = g.Name
- })
- .OrderBy(item => item.GroupName)
- .ToList();
- //Linq形式1
- var clients = from c in ctx.Clients
- from g in ctx.Groups
- where c.GroupID == g.ID
- orderby g.Name
- select new
- {
- c.ID,
- c.ComputerName,
- GroupName = g.Name
- };
- //Linq形式2
- var clients = from c in ctx.Clients
- where c.Status == 1
- join g in ctx.Group
- on c.GroupID equals g.ID into result
- from r in result
- order by r.Name
- select new
- {
- c.ID,
- c.ComputerName,
- GroupName = r.Name
- };
6、分页
SQL:
- -- 方案1
- SELECT TOP 10 * FROM [Clients] WHERE Status=1
- AND ID NOT IN
- (
- SELECT TOP 20 ID FROM [Clients] WHERE Status=1
- ORDER BY ComputerName
- )
- ORDER BY ComputerName
- --方案2
- SELECT * FROM
- (
- SELECT *, ROW_NUMBER() OVER(ORDER BY ComputerName) AS RowNo
- FROM [Clients]
- WHERE Status=1
- )t
- WHERE RowNo >= 20 AND RowNo < 30
EF:
- //Func形式
- var clients = ctx.Clients.Where(c => c.Status=1)
- .OrderBy(c => c.ComputerName)
- .Skip(20)
- .Take(10)
- .ToList();
- //Linq形式
- var clients = (from c in ctx.Clients
- orderby c.ComputerName
- select c).Skip(20).Take(10);
7、分组统计:
SQL:
- SELECT Status,COUNT(*) AS Cnt FROM [Clients]
- GROUP BY Status
- ORDER BY COUNT(*) DESC
EF:
- //Func形式
- var result = ctx.Clients.GroupBy(c => c.Status)
- .Select(s => new
- {
- Status = s.Key,
- Cnt = s.Count()
- })
- .OrderByDescending(r => r.Cnt);
- //Linq形式
- var result = from c in ctx.Clients
- group c by c.Status into r
- orderby r.Count() descending
- select new
- {
- Status = r.Key,
- Cnt = r.Count()
- };
Entity Framework查询的更多相关文章
- Entity Framework查询原理
Entity Framework查询原理 前言 Entity Framework的全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Rela ...
- Entity Framework查询生成大量的子查询,如何避免?求救
最近使用Entity Framework做一个中型的项目,一张表含有千万条数据,并没有使用很复杂的查询,只是程序上使用了DTO进行帅选数据,且使用了分页,效果很不理想.经过跟踪sql,我发现很多简单的 ...
- 如何得到EF(ADO.NET Entity Framework)查询生成的SQL? ToTraceString Database.Log
ADO.NET Entity Framework ToTraceString //输出单条查询 DbContext.Database.Log //这里有详细的日志
- C# Entity Framework查询小技巧 NoTracking
在使用Entity Framework做查询的时候,如果只需要显示,而不用保存实体,那么可以用AsNoTracking()来获取数据. 这样可以提高查询的性能. 代码如下: var context = ...
- Entity Framework查询注意
首先我们看下where的方法,直接查看定义(定义如下),其实一种是对IEnumerable的扩展,一种是对IQueryable的扩展,直接看最常用的,其实区别就在IEnumerable的扩展的参数是系 ...
- [原创]Entity Framework查询原理
前言 Entity Framework的全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Relational Mapping)框架.Ent ...
- 解决Entity Framework查询匿名对象后的跨域访问的一种方式
在Entity Framework中,可以使用lambda表达式进行对数据的查询,而且可以将查询结果直接映射为对象或者对象列表,这极大的提高的开发速度,并且使数据层的数据更加方便处理和传递.但是很多时 ...
- 一个最简单的使用Entity Framework 查询SQL 数据库的例子
1.ADO.NET 3.5 Entity Framework是随着.net framework 3.5一起发布的,确认开发环境版本是大于等于3.5版本 2.确认已经安装了ADO.NET 3.5 Ent ...
- Entity Framework查询,EF执行SQl
一.简介 EF 支持开放底层的 ADO.NET 框架,DbContext有三种常用方法 DbSet.SqlQuery //查询并返回Entities DbContext.Database.SqlQue ...
随机推荐
- c++11のunique_lock和once_flag
一. Unique _lock和lockguard一样,到那时比lockguard更加灵活,可以随时按照需要加锁开锁 std::unique_lock<std::mutex> locker ...
- Zabbix 3.4.7调整监控阈值以及告警级别
1.找到需要监控的主机:右上角进行搜索 我们要更改sepm02p的阈值和级别: 进行更改级别:先点击Triggers , 选中要更改的监控项,例如我要更改CPU,点击以下红色标出的,千万不要选择Tem ...
- C# — 调用dll出现试图加载不正确格式的程序问题
今天在调用百度dll包时,运行项目出现了如下警告: 修改:鼠标右击项目名称----选择属性----生成-----平台目标-----X64(由于我调用的是X64的dll包,所以这里选择X64,网上许多说 ...
- Luogu P1852 BZOJ 2144 [国家集训队]跳跳棋
qwq 这题一看就不会,如果不是gg让做我是坚决不会做的 画图模拟,因为一次只能跳过一个棋子,所以对于一种情况只有三种移动方式: 中间向左跳 中间向右跳 左或右(距中间近的那个)向中间跳 发现,除了跳 ...
- 全文搜索引擎 ElasticSearch 还是 Solr?
最近项目组安排了一个任务,项目中用到了全文搜索,基于全文搜索 Solr,但是该 Solr 搜索云项目不稳定,经常查询不出来数据,需要手动全量同步,而且是其他团队在维护,依赖性太强,导致 Solr 服务 ...
- mysql常见问题处理
出现: Access denied for user ''@'localhost' to database ' 2.error: Found option without preceding grou ...
- Webdriver获取多个元素
官方通过如下代码获取多个元素: List<WebElement> inputs = driver.findElements(By.xpath("//input")); ...
- js中“==”与“===”区别
直接上代码 if(2==='2'){ console.log(true) }else{ console.log(false) } //打印结果 false if(2=='2'){ console.lo ...
- HTTP/1.0 vs HTTP/1.1 vs HTTP/2
HTTP 1.0 vs 1.1 Proxy support and the Host field: HTTP 1.1 has a required Host header by spec. HTTP ...
- docker_监控
目录 Docker 自带的监控子命令 sysdig Weave Scope cAdvisor Prometheus Prometheus 的架构 多维数据模型 Docker 自带的监控子命令 ps d ...