ASP.NET EF(LINQ/Lambda查询)

Posted DrHao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET EF(LINQ/Lambda查询)相关的知识,希望对你有一定的参考价值。

EF(EntityFrameWork) ORM(对象关系映射框架/数据持久化框架),根据实体对象操作数据表中数据的一种面向对象的操作框架,底层也是调用ADO.NET

ASP.NET MVC 项目会自动导入MVC程序集,因为默认.NET环境(GAC)中没有这个程序集

 1 create database MyFirstEF
 2 on primary
 3 (
 4     name=\'MyFirstEF.mdf\',
 5     --修改为自己电脑上SQL DB路径
 6     filename=\'E:\\ProgramMSSQLServerDB\\MyFirstEF.mdf\',
 7     size=5mb,
 8     maxsize=100mb,
 9     filegrowth=10%
10 )
11 log on
12 (
13     name=\'MyFirstEF_log.ldf\',
14     --修改为自己电脑上SQL DB路径
15     filename=\'E:\\ProgramMSSQLServerDB\\MyFirstEF_log.ldf\',
16     size=2mb,
17     maxsize=100mb,
18     filegrowth=5mb
19 )
20 go
21 
22 use MyFirstEF
23 go
24 
25 create table CustomerInfo
26 (
27     id int identity(1,1) primary key,
28     customerName nvarchar(100) not null,
29     customerDate datetime
30 )
31 go
32 
33 insert into CustomerInfo values(\'aaaaa\',getdate())
34 go
35 
36 select * from CustomerInfo
37 go
38 
39 create table OrderInfo
40 (
41   id int identity(1,1) primary key,
42   orderName nvarchar(100),
43   customerId int
44 )
45 go
46 
47 
48 alter table OrderInfo
49 add constraint FK_OrderInfo_CustomerInfo
50 foreign key(customerId) references CustomerInfo(id)
51 on delete cascade
52 on update cascade
53 
54 go
55 
56 
57 select * from CustomerInfo
58 select * from OrderInfo
create SQL

1:LINQ[Language Integrated Query]/Lambda 基本查询

DbContext context = new MyFirstEFEntities();
// LINQ 基本查询
var rows = from c in context.Set<CustomerInfo>()
           select c;
//返回的是SQL文
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

//Lambda 基本查询
var rows1 = context.Set<CustomerInfo>().Select(c => c);
Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());

对应SQL为:

--基本查询
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent1].[customerDate] AS [customerDate]
FROM [dbo].[CustomerInfo] AS [Extent1]

2:LINQ/Lambda 单条件查询

DbContext context = new MyFirstEFEntities();

//LINQ 单条件查询
var rows = from c in context.Set<CustomerInfo>()
           where c.id > 2
           select c;
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

//Lambda 单条件查询
var rows1 = context.Set<CustomerInfo>().Where(c => (c.id > 2));
Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());

对应SQL为:

--单条件查询
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent1].[customerDate] AS [customerDate]
FROM [dbo].[CustomerInfo] AS [Extent1]
WHERE [Extent1].[id] > 2

3:LINQ/Lambda 多条件查询

DbContext context = new MyFirstEFEntities();

//LINQ 多条件查询
var rows = from c in context.Set<CustomerInfo>()
           where c.id > 2 && c.customerName.Contains("today")
           select c;
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

//Lambda 多条件查询
var rows1 = context.Set<CustomerInfo>().Where(c => (c.id > 2) && (c.customerName.Contains("today")));
Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());

对应SQL为:

--多条件查询
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent1].[customerDate] AS [customerDate]
FROM [dbo].[CustomerInfo] AS [Extent1]
WHERE ([Extent1].[id] > 2) AND ([Extent1].[customerName] LIKE N\'%today%\')

4:LINQ/Lambda 连接查询

DbContext context = new MyFirstEFEntities();

//LINQ 连接查询
var rows = from c in context.Set<CustomerInfo>()
           join o in context.Set<OrderInfo>() on c.id equals o.customerId
           select c;
//连接查询相当于是Inner join
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

//Lambda 连接查询
var rows1 = context.Set<CustomerInfo>().Join(context.Set<OrderInfo>(), c => c.id, o => o.customerId, (c, o) => c);
Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());

对应SQL为:

--连接查询
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent1].[customerDate] AS [customerDate]
FROM  [dbo].[CustomerInfo] AS [Extent1]
INNER JOIN [dbo].[OrderInfo] AS [Extent2] ON [Extent1].[id] = [Extent2].[customerId]

5:LINQ:多from查询:专用于有导航属性的查询(LINQ特有)

DbContext context = new MyFirstEFEntities();

//多from查询:专用于有导航属性的查询(LINQ特有)
//select CustomerInfo
var rows = from c in context.Set<CustomerInfo>()
           from o in c.OrderInfoes
           where c.id > 2 && o.orderName.Contains("car")
           select c;
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

//select OrderInfo
var rows1 = from c in context.Set<CustomerInfo>()
           from o in c.OrderInfoes
           where c.id > 2 && o.orderName.Contains("car")
           select o;

Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());

对应SQL为:

--多from查询:专用于有导航属性的查询(LINQ特有)
--select CustomerInfo
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent1].[customerDate] AS [customerDate]
FROM  [dbo].[CustomerInfo] AS [Extent1]
INNER JOIN [dbo].[OrderInfo] AS [Extent2] ON [Extent1].[id] = [Extent2].[customerId]
WHERE ([Extent1].[id] > 2) AND ([Extent2].[orderName] LIKE N\'%car%\')

--select OrderInfo
SELECT
[Extent1].[id] AS [id],
[Extent1].[orderName] AS [orderName],
[Extent1].[customerId] AS [customerId]
FROM [dbo].[OrderInfo] AS [Extent1]
WHERE ([Extent1].[customerId] IS NOT NULL) AND ([Extent1].[customerId] > 2) AND ([Extent1].[orderName] LIKE N\'%car%\')

6:LINQ/Lambda 查询部分列(指定列)

DbContext context = new MyFirstEFEntities();

//LINQ 查询部分列(指定列)
var rows = from c in context.Set<CustomerInfo>()
           select new { Id = c.id, Name = c.customerName };
Console.WriteLine(rows);
foreach (var row in rows)
{
    Console.WriteLine(row.Id + ":" + row.Name);
}

//Lambda 查询部分列(指定列)
var rows1 = context.Set<CustomerInfo>().Select(c => (new { Id = c.id, Name = c.customerName }));
foreach (var row in rows1)
{
    Console.WriteLine(row.Id + ":" + row.Name);
}

对应SQL为:

--查询部分列(指定列)
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName]
FROM [dbo].[CustomerInfo] AS [Extent1]

7:LINQ/Lambda 查询多个表格部分列(指定列)

DbContext context = new MyFirstEFEntities();

//LINQ 查询多个表格部分列
var rows = from c in context.Set<CustomerInfo>()
           join o in context.Set<OrderInfo>()
           on c.id equals o.customerId
           select new { CustomerName = c.customerName, OrderName = o.orderName };
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

var rows1 = context.Set<CustomerInfo>().Join(context.Set<OrderInfo>(), c => c.id, o => o.customerId, (c, o) => (new { CustomerName = c.customerName, OrderName = o.orderName }));
Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());

对应SQL为:

--查询多个表格部分列(指定列)
SELECT
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent2].[orderName] AS [orderName]
FROM  [dbo].[CustomerInfo] AS [Extent1]
INNER JOIN [dbo].[OrderInfo] AS [Extent2] ON [Extent1].[id] = [Extent2].[customerId]

8:Lambda:分页查询 lambda特有 OrderBy/Skip/Take 

DbContext context = new MyFirstEFEntities();

int pageSize = 2;
int pageIndex = 2;
//需要构建出startIndex 和 EndedIndex
var rows = context.Set<CustomerInfo>().OrderBy(c => c.id).Skip(pageSize * (pageIndex - 1)).Take(2);
Console.WriteLine(rows);
Console.WriteLine(rows.Count());

对应SQL为:

--分页查询 lambda特有 
SELECT TOP (2)
[Extent1].[id] AS [id],
[Extent1].[customerName] AS [customerName],
[Extent1].[customerDate] AS [customerDate]
FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number]
        FROM [dbo].[CustomerInfo] AS [Extent1]
)  AS [Extent1]
WHERE [Extent1].[row_number] > 2
ORDER BY [Extent1].[id] ASC

9:Lambda:封装分页查询

DbContext context1 = new MyFirstEFEntities();

int pageSize = 2;
int pageIndex = 2;

//var rows = context1.Set<CustomerInfo>().Where(c => c.id > 1).OrderBy(c => c.id).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
//Console.WriteLine(rows);
//Console.WriteLine(rows.Count());

var rows1 = GetPageList<CustomerInfo, int>(context1, pageSize, pageIndex, c => c.id > 1, c => c.id);
Console.WriteLine(rows1);
Console.WriteLine(rows1.Count());
//泛型 委托 Lambda表达式
public static IQueryable<T> GetPageList<T, Tkey>(DbContext context, int pageSize, int pageIndex, Expression<Func<T, bool>> where, Expression<Func<T, Tkey>> orderBy) where T : class
{
    var rows = context.Set<T>() //泛型约束,因为上下文的Set<T>()方法,对T有要求
        .Where(where)  //语法糖 将Lambda表达式封装为Expresson对象
        .OrderBy(orderBy)
        .Skip(pageSize * (pageIndex - 1))
        .Take(pageSize);
    return rows;
}

对应SQL为:

SELECT TOP (2)
[Filter1].[id] AS [id],
[Filter1].[customerName] AS [customerName],
[Filter1].[customerDate] AS [customerDate]
FROM ( SELECT [Extent1].[id] AS [id], [Extent1].[customerName] AS [customerName], [Extent1].[customerDate] AS [customerDate], row_number() OVER (ORDER BY [Extent1].[id] ASC) AS [row_number]
        FROM [dbo].[CustomerInfo] AS [Extent1]
        WHERE [Extent1].[id] > 如何将带有内连接的 sql 查询转换为 linq lambda 表达式?

ASP.NET EF6 查询模型

ASP.NET Mvc开发之EF延迟加载

asp.net EF学习系列----深入理解查询延迟加载技术

asp.net mvc+ef框架下的多条件查询

ASP.NET MVC 4、EF 和 LINQ:为多对多关系创建特定的 LINQ 查询