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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM