走進Linq--Linq橫空出世篇
系列文章導航:
不能不說的C#特性-迭代器(下),yield以及流的延遲計算
走進Linq-Linq to SQL How do I(1)
走進Linq-Linq to SQL How do I(2)
走進Linq-Linq to SQL How do I(3)
走進Linq-Linq to SQL源代碼賞析 Table的獲取過程
走進Linq-Linq to SQL源代碼賞析之Provider的初始化
走進Linq-Linq to SQL源代碼賞析,通過Linq to SQL看Linq
某日編程大師云游到某處,見一剛畢業不久學過兩天C#和兩天SQL的coder在那里發牢騷,為啥我要寫這么多for,這么多if才能查詢出我需要的數據,為啥我不能像SQL那樣,發送一條命令告訴數據庫我需要啥樣的數據,它就給我返回來。
編程大師如是說:傻小子,像SQL那叫第四代編程語言,常存在于象牙塔和研究所里面的學究語言,還有個高雅的名字:函數編程。它只需要你告訴它要什么,而不需要告訴它怎么做。而你使用的C#語言屬于命令式編程,你必須像發送命令一樣一步步的告訴你的機器怎么做。
發牢騷的coder回了一句:不懂,我只是想不通,數據庫能做這樣的處理,為啥C#這么牛的語言不能呢。 編程大師心里想著:這是不可能的事情,因為C#它是強類型語言,)*&)(&)*)()*&%&%&^(后面省去200字)。
天色還未晚,編程大師就急匆匆的回家了,他心里一直記著那位發牢騷的coder的話:為什么不能,為什么不能。
晚上,編程大師做了一個夢,一個奇怪的夢,他的師傅“白眉”只說了三個字母:DSL。
編程大師想著,DSL,領域專用語言,師傅要對我說什么呢,難道和今天我遇見的事有關?
上面這段文字是一段調侃,調節一下氣氛,呵呵。
我覺得Linq就是一種DSL,在C#等常規語言上抽象起來的,面向數據處理領域的特定“語言”,當然,它的根基還是這些常規語言。
select,from,where,group等關鍵字本來只是在SQL里出現,現在把它們引入到C#這些常規編程語言中。
那C#等是如何做到的呢?是在CLR底層支持的么?不是。既然“編譯器”可以將C#編譯成MSIL,那為什么編譯不能干更多一點事情?將這些為了領域編程而出現關鍵字編譯成原始語法。
下面還是從實例來說明吧:
我們有一個圖書類Book,先已經有一個填充有數據的Book集合,我們需要從這個集合里查找出單價小于50的書籍:
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://imageproxy.pixnet.cc/imgproxy?url=https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)