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

 

 

 先在数据库创建一个数据库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

posted @   DrHao  阅读(3942)  评论(7编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
点击右上角即可分享
微信分享提示