您的位置:首页 > 编程语言 > ASP

ASP.NET EF 使用LinqPad 快速学习Linq

2018-01-30 16:18 726 查看

ASP.NET EF 使用LinqPad 快速学习Linq

使用LinqPad这个工具可以很快学习并掌握linq[Language Integrated Query]

linqPad官方下载地址:http://www.linqpad.net/


linqPad4百度云下载(for .NET Framework4.0/4.5):链接:http://pan.baidu.com/s/1gflmRDp 密码:3n3f

linqPad5百度云下载(for .NET Framework 4.6):链接:http://pan.baidu.com/s/1dE5Z0VB 密码:qpgc

LINQPad is not just for LINQ queries, but any C#/F#/VB expression, statement block or program. Put an end to those hundreds of Visual Studio Console projects cluttering your source folder and join the revolution of LINQPad scripters and incremental developers.

Reference your own assemblies and NuGet packages. Prototype your ideas in LINQPad and then paste working code into Visual Studio. Or call your scripts directly from the command-line.

Experience LINQPad’s rich output formatting, optional debugger and autocompletion, and the magic of dynamic development and instant feedback!

LINQPad 并非只为 LINQ 查询,但任何 C# /F #/VB 表达式、 语句块或程序。结束这些数百个视觉工作室控制台项目塞满您的源文件夹和参加革命的LINQPad 脚本编写者和增量的开发人员。
引用你自己的程序集和 NuGet 程序包。原型的你的想法在 LINQPad,然后粘贴工作代码到 Visual Studio。或直接从命令行调用您的脚本。

体验 LINQPad 的丰富的输出格式、 可选的调试器和自动完成和神奇的动态发展和即时反馈 !

create database MyFirstEF
on primary
(
name='MyFirstEF.mdf',
--修改为自己电脑上SQL DB路径
filename='E:\ProgramMSSQLServerDB\MyFirstEF.mdf',
size=5mb,
maxsize=100mb,
filegrowth=10%
)
log on
(
name='MyFirstEF_log.ldf',
--修改为自己电脑上SQL DB路径
filename='E:\ProgramMSSQLServerDB\MyFirstEF_log.ldf',
size=2mb,
maxsize=100mb,
filegrowth=5mb
)
go

use MyFirstEF
go

create table CustomerInfo
(
id int identity(1,1) primary key,
customerName nvarchar(100) not null,
customerDate datetime
)
go

create table OrderInfo
(
id int identity(1,1) primary key,
orderName nvarchar(100),
customerId int
)
go

insert into CustomerInfo
select 'aa',GETDATE() union all
select 'bb',GETDATE() union all
select 'cc',GETDATE() union all
select 'dd',GETDATE()
go

insert into OrderInfo
select 'bike1',2 union all
select 'bike2',2 union all
select 'car1',3 union all
select 'car2',3 union all
select 'chezi1',4 union all
select 'chezi2',4 union all
select 'test1',5 union all
select 'test2',5
go

select * from CustomerInfo
go

select * from OrderInfo
go


安装完毕linqPad之后,打开软件 --Add Connection-->Build data context automatically(Default(LINQ to SQL))





我们在linqPad的query标签里把Language 选择为c# Expression ,把Connection 选择数据MyFirstEF



1:Linq left join(left join 是Left outer join 简写)

在面板中输入Linq,点击运行或者直接按F5【注意CustomerInfo/OrderInfo及字段 都需要按照EF中的格式写(不能按照数据库格式)】

from c in CustomerInfo
join o in OrderInfo
on c.Id equals o.CustomerId
into MyLeftJoin
from tt in MyLeftJoin.DefaultIfEmpty()
select new
{
cname=c.CustomerName,
//这里主要第二个集合有可能为空。需要判断
//oname=tt==null?"":tt.OrderName
oname=tt.OrderName
}


对应SQL为:

SELECT [t0].[customerName] AS [cname], [t1].[orderName] AS [oname]
FROM [CustomerInfo] AS [t0]
LEFT OUTER JOIN [OrderInfo] AS [t1] ON ([t0].[id]) = [t1].[customerId]


对应lambda表达式为:

CustomerInfo
.GroupJoin (
OrderInfo,
c => (Int32?)(c.Id),
o => o.CustomerId,
(c, MyLeftJoin) =>
new
{
c = c,
MyLeftJoin = MyLeftJoin
}
)
.SelectMany (
temp0 => temp0.MyLeftJoin.DefaultIfEmpty (),
(temp0, tt) =>
new
{
cname = temp0.c.CustomerName,
oname = tt.OrderName
}
)




2:Linq right join(right join 是Right outer join 简写)[最后生成SQL还是left join]

在面板中输入Linq,点击运行或者直接按F5

from o in OrderInfo
join c in CustomerInfo
on o.CustomerId equals c.Id
into MyRightJoin
from tt in MyRightJoin.DefaultIfEmpty()
select new
{
//这里集合有可能为空。需要判断
//cname=tt==null?"":tt.CustomerName,
cname=tt.CustomerName,
oname=o.OrderName
}


对应SQL为:

SELECT [t1].[customerName] AS [cname], [t0].[orderName] AS [oname]
FROM [OrderInfo] AS [t0]
LEFT OUTER JOIN [CustomerInfo] AS [t1] ON [t0].[customerId] = ([t1].[id])


对应lambda表达式为:

OrderInfo
.GroupJoin (
CustomerInfo,
o => o.CustomerId,
c => (Int32?)(c.Id),
(o, MyRightJoin) =>
new
{
o = o,
MyRightJoin = MyRightJoin
}
)
.SelectMany (
temp0 => temp0.MyRightJoin.DefaultIfEmpty (),
(temp0, tt) =>
new
{
cname = tt.CustomerName,
oname = temp0.o.OrderName
}
)




3:Linq inner join

在面板中输入Linq,点击运行或者直接按F5

from c in CustomerInfo
join o in OrderInfo
on c.Id equals o.CustomerId
select new
{
cname=c.CustomerName,
oname=o.OrderName
}


对应SQL为:

SELECT [t0].[customerName] AS [cname], [t1].[orderName] AS [oname]
FROM [CustomerInfo] AS [t0]
INNER JOIN [OrderInfo] AS [t1] ON ([t0].[id]) = [t1].[customerId]


对应lambda表达式为:

CustomerInfo
.Join (
OrderInfo,
c => (Int32?)(c.Id),
o => o.CustomerId,
(c, o) =>
new
{
cname = c.CustomerName,
oname = o.OrderName
}
)




暂时就到这里,其他的参考官方文档。

参考链接:

ASP.NET MVC EF直接更新数据(不需查询):http://www.cnblogs.com/Dr-Hao/p/5255630.html

ASP.NET EF(LINQ/Lambda查询):http://www.cnblogs.com/Dr-Hao/p/5356928.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐