使用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 的丰富的输出格式、 可选的调试器和自动完成和神奇的动态发展和即时反馈 !

先在数据库创建一个数据库MyFirstEF 和CustomerInfo和OrderInfo两张表

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

--create SQL

安装完毕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

ASP.NET EF 使用LinqPad 快速学习Linq的更多相关文章

  1. js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq

    js_html_input中autocomplete="off"在chrom中失效的解决办法 分享网上的2种办法: 1-可以在不需要默认填写的input框中设置 autocompl ...

  2. linqPad快速学习LINQ(含视频)

    在这里我向大家推荐的一个具是LinqPad有了这个工具并熟练使用就可以很快学习并掌握linq 安装步骤: 使用LINQPad可以很方便的调试linq以及lambda表达式.其中自带了linq以及F#简 ...

  3. LinqPad工具:帮你快速学习Linq

    LinqPad工具:帮你快速学习Linq 参考: http://www.cnblogs.com/li-peng/p/3441729.html ★:linqPad下载地址:http://www.linq ...

  4. 用linqPad帮助你快速学习LINQ

    在这里我向大家推荐的一个具是LinqPad有了这个工具并熟练使用就可以很快学习并掌握linq linqPad下载地址:http://www.linqpad.net/ 它也自带了很多例子方便大家查询,l ...

  5. linqPad帮助你快速学习LINQ

    linqPad http://www.cnblogs.com/li-peng/p/3441729.html http://www.linqpad.net/ Linqer http://www.sqlt ...

  6. ASP.NET EF(LINQ/Lambda查询)

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

  7. ASP.NET快速学习方案(.NET菜鸟的成长之路)

    想要快速学习ASP.NET网站开发的朋友可以按照下面这个学习安排进度走.可以让你快速入门asp.net网站开发!但也局限于一般的文章类网站!如果想学习更多的技术可以跟着我的博客更新走!我也是一名.NE ...

  8. 学习LINQ,发现一个好的工具。LINQPad!!

    今日学习LINQ,发现一个好的工具.LINQPad!! 此工具的好处在于,不需要在程序内执行,直接只用工具测试.然后代码通过即可,速度快,简洁方便. 可以生成其LINQ查询对应的lambda和SQL语 ...

  9. 使用ASP.NET MVC+Entity Framework快速搭建系统

    详细资料: http://www.cnblogs.com/dingfangbo/p/5771741.html 学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和 ...

随机推荐

  1. (十)Jmeter中的Debug Sampler介绍

    一.Debug Sampler介绍: 使用Jmeter开发脚本时,难免需要调试,这时可以使用Jmeter的Debug Sampler,它有三个选项:JMeter properties,JMeter v ...

  2. artdialog对话框 三种样式 网址:http://www.planeart.cn/demo/artDialog/_doc/labs.html

    摇头效果 类似与wordpress登录失败后登录框可爱的左右晃动效果 // 2011-07-17 更新 artDialog.fn.shake = function (){ var style = th ...

  3. 【Python】Python的time和datetime模块

    time 常用的有time.time()和time.sleep()函数. import time print(time.time()) 1499305554.3239055 上面的浮点数称为UNIX纪 ...

  4. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  5. 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

    题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...

  6. Mybatis笔记四:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'

    错误异常:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for pr ...

  7. bzip2 --安装

    下载源文件安装包: http://www.bzip.org/downloads.html 解压: tar -xzvf bzip2-1.0.6.tar.gz 进入解压后的目录: cd bzip2-1.0 ...

  8. NOIP2017 Day2 T3 列队(treap)

    可以直接用treap上大模拟...n+1个treap维护n行的前m-1个点和最后一列. 需要支持删除一个点或者一段区间,而空间并不支持存下所有的点的时候,可以用一个点代替一个区间,记录区间首项的值和区 ...

  9. 【bzoj4559】成绩比较

    Portal -->bzoj4559 补档计划 ​  借这题补个档--拉格朗日插值 ​​  插值的话大概就是有一个\(n-1\)次多项式\(A(x)\),你只知道它在\(n\)处的点值,分别是\ ...

  10. python基础----常用模块

    一 time模块(时间模块)★★★★                                                      时间表现形式 在Python中,通常有这三种方式来表示时 ...