本系列文章
走进Linq-Linq横空出世篇
走进Linq-辉煌的背后
走进Linq-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,那为什么编译不能干更多一点事情?将这些为了领域编程而出现关键字编译成原始语法。
![](https://i-blog.csdnimg.cn/blog_migrate/36494d69744c89b836a1e1fbfeb3c718.jpeg)
下面还是从实例来说明吧:
我们有一个图书类Book,先已经有一个填充有数据的Book集合,我们需要从这个集合里查找出单价小于50的书籍:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
{
public static IList < Book > SearchBookByPrice()
{
IList < Book > books = //
![](https://www.cnblogs.com/Images/dot.gif)
IList < Book > results = new List < Book > ();
foreach (Book book in books)
{
if (book.Price < 50 )
{
results.Add(book);
}
}
return results;
}
}
{
public delegate bool Condtion(Book book);
public static IList < Book > SearchBook(Condtion condition)
{
IList < Book > books = //
![](https://www.cnblogs.com/Images/dot.gif)
IList < Book > results = new List < Book > ();
foreach (Book book in books)
{
if (condition(book))
{
results.Add(book);
}
}
return results;
}
}
{
return book.Title == " yuyi " ;
}
IList < Book > results = Helper.SearchBook( new Condition(ConditionTitle));
不过这里还有一点不爽,每一次调用这个查找方法都要带Helper ,要是IList<Book> 自己就这个方法该多好。这个想法很好,C# 3.0 里还为我们提供了扩展方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
{
public delegate bool Condtion < T > (T t);
public static IEnumerable < T > FindBy < T > ( this IEnumerable < T > items, Condtion < T > condition)
{
foreach (T t in items)
{
if (condition(t))
{
// C# 2.0里出现的一个关键字,返回一个迭代器
yield return t;
}
}
}
}
但是做集合操作的时候我们不仅仅需要Where ,还需要OrderBy ,Group 等等,我想把所有的SQL 能干的都移植过来。当然微软也意识到了这点,如是在.net 3.5 里,微软发布了我们梦寐以求的Linq ,将查询集成到语言里面来。它给IEnumerable<T> 添加了很多扩展方法,这样你可以很自然的去调用。你可以使用Reflector 打开System.Core.dll ,打开System.Linq 命名空间,在这个命名空间里有一个Enumerable 类,这里面就是微软为我们添加的扩展方法,看看,是不是SQL 里所有的东西都可以在这里找到了。
好了,就此搁笔吧,这一篇作为我的走进Linq系列的开篇,在接下来我会为你把Linq大卸八块。