SlideShare a Scribd company logo
1 of 19
Download to read offline
Linq 介绍


http://greenerycn.cnblogs.com
          2010-10-19
内容

 从Foreach 到 Linq

 Linq Example

 Resharper插件
What is Linq
 LINQ,语言级集成查询
 (Language INtegrated Query)



 仿照sql语言,实现在数组、list等可枚丼的数据集
 中进行数据查找、排序



 提高开发人员进行 查询信息的效率
Linq




       今日主题
ForQuery
查询一个int 数组中小于5的数字,然后保存到一个list中


int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };
var below5list = new List<int>();

foreach (var i in arr)
{
       if (i < 5)
       {
             below5list.Add(i);
       }
}
LinqQuery
int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };



var below5list = from i in arr
                 where i < 5
                 select i;
为啥From在前
 为了智能感应(Intelisence)
   Select p.Name, p.Age From p In persons Where xxx



 输入到p的时候,还不知p的类型

 改一下:
  From p in persons
  where xxx
  select p.Name,p.Age
复杂点的Linq
 按照字符长短,分类罗列出来

    "Java","C#","C++","Delphi","VB.net","VC.net","
    C++ Builder","Kylix","Perl","Python"

   2
         C#
   3
         C++
   4
         Java
         Perl
   5:
         VB.net
Code
string[] languages = { "Java", "C#", "C++", "Delphi",
"VB.net", "VC.net", "C++ Builder", "Kylix", "Perl",
"Python" };



var query = from item in languages
             orderby item
             group item by item.Length into lengthGroups
             orderby lengthGroups.Key descending
             select lengthGroups;
Linq用途

 主要是查找

  筛选

  排序

  聚合

 让代码文字化
常用查询运算符
   筛选数据: Where,OfType

   投影运算: Select, SelectMany

   对数据进行排序: OrderBy, ThenBy

   数据分区: GroupBy

   限定符运算: Any, All

   数据分区: Take, Skip, TakeWhile, SkipWhile

   Set 操作: Distinct, Union, Intersect, Except

   聚合运算: Count, Sum, Min, Max, Average

   转换数据类型: ToArray, ToList, ToDictionary
例子: 查字符串中的数字
string aString = "ABCDE99F-J74-12-89A";

var stringQuery = from ch in aString
                  where Char.IsDigit(ch)
                  select ch;
例子:文本文件中的差异
var names1 = System.IO.File.ReadAllLines(@"names1.txt");

var names2 = System.IO.File.ReadAllLines(@"names2.txt");



// 在names1中但不再names2中的行

// 就是求 差集

var differenceQuery =

  names1.Except(names2);
例子:单词是否都含r字母
string[] words = { "believe", "relief", "receipt",
"field" };

bool isWordsAllContainsRLetter = words.Any(w =>
w.Contains("r"));
Resharper
 VS2008 插件

 辅助编写代码

 提供普通for循环代码到linq代码的转换
ReSharper
foreach (var i in arr)
{
        if (i < 5)
        {
                below5list.Add(i);
        }
}




below5list.AddRange(arr.Where(i => i < 5))
Lambda表达式
 arr.Where(i => i < 5)
                          匿名方法



   bool Function(var i)
   {
     return i < 5;
   }                       隐式声明
参考资料

 LINQ 介绍

 101 LINQ Samples

 C# 中的 LINQ 入门
谢谢!

More Related Content

What's hot

Lucene2 4学习笔记1
Lucene2 4学习笔记1Lucene2 4学习笔记1
Lucene2 4学习笔记1yiditushe
 
中心教员Java面试题1
中心教员Java面试题1中心教员Java面试题1
中心教员Java面试题1yiditushe
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析wavefly
 
快速了解PostgreSQL
快速了解PostgreSQL快速了解PostgreSQL
快速了解PostgreSQL正中 周
 
Elastic stack day-2
Elastic stack day-2Elastic stack day-2
Elastic stack day-2YI-CHING WU
 
Redis 内存存储结构分析 -wuzhu--20110418
Redis 内存存储结构分析 -wuzhu--20110418Redis 内存存储结构分析 -wuzhu--20110418
Redis 内存存储结构分析 -wuzhu--20110418tidesq
 
[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走台灣資料科學年會
 
Thdl 系統功能簡介
Thdl 系統功能簡介Thdl 系統功能簡介
Thdl 系統功能簡介thdl
 
考虑词语连接查询的索引技术研究
考虑词语连接查询的索引技术研究考虑词语连接查询的索引技术研究
考虑词语连接查询的索引技术研究weedge
 
12, string
12, string12, string
12, stringted-xu
 
主機管理基本指令練習(四)
主機管理基本指令練習(四)主機管理基本指令練習(四)
主機管理基本指令練習(四)K- Peggy
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享晓锋 陈
 
An introduction to inverted index
An introduction to inverted indexAn introduction to inverted index
An introduction to inverted indexweedge
 
淘宝数据魔方的系统架构 -长林
淘宝数据魔方的系统架构 -长林淘宝数据魔方的系统架构 -长林
淘宝数据魔方的系统架构 -长林Shaoning Pan
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeAngel Boy
 
千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7Justin Lin
 
Pytables
PytablesPytables
Pytablesgowell
 
Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Angel Boy
 
CH14:NIO 與 NIO2
CH14:NIO 與 NIO2CH14:NIO 與 NIO2
CH14:NIO 與 NIO2Justin Lin
 

What's hot (20)

Lucene2 4学习笔记1
Lucene2 4学习笔记1Lucene2 4学习笔记1
Lucene2 4学习笔记1
 
[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰
 
中心教员Java面试题1
中心教员Java面试题1中心教员Java面试题1
中心教员Java面试题1
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
 
快速了解PostgreSQL
快速了解PostgreSQL快速了解PostgreSQL
快速了解PostgreSQL
 
Elastic stack day-2
Elastic stack day-2Elastic stack day-2
Elastic stack day-2
 
Redis 内存存储结构分析 -wuzhu--20110418
Redis 内存存储结构分析 -wuzhu--20110418Redis 内存存储结构分析 -wuzhu--20110418
Redis 内存存储结构分析 -wuzhu--20110418
 
[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走[系列活動] Python 程式語言起步走
[系列活動] Python 程式語言起步走
 
Thdl 系統功能簡介
Thdl 系統功能簡介Thdl 系統功能簡介
Thdl 系統功能簡介
 
考虑词语连接查询的索引技术研究
考虑词语连接查询的索引技术研究考虑词语连接查询的索引技术研究
考虑词语连接查询的索引技术研究
 
12, string
12, string12, string
12, string
 
主機管理基本指令練習(四)
主機管理基本指令練習(四)主機管理基本指令練習(四)
主機管理基本指令練習(四)
 
Mongo db技术分享
Mongo db技术分享Mongo db技术分享
Mongo db技术分享
 
An introduction to inverted index
An introduction to inverted indexAn introduction to inverted index
An introduction to inverted index
 
淘宝数据魔方的系统架构 -长林
淘宝数据魔方的系统架构 -长林淘宝数据魔方的系统架构 -长林
淘宝数据魔方的系统架构 -长林
 
Linux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledgeLinux binary Exploitation - Basic knowledge
Linux binary Exploitation - Basic knowledge
 
千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7千呼萬喚始出來的 Java SE 7
千呼萬喚始出來的 Java SE 7
 
Pytables
PytablesPytables
Pytables
 
Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation Linux Binary Exploitation - Heap Exploitation
Linux Binary Exploitation - Heap Exploitation
 
CH14:NIO 與 NIO2
CH14:NIO 與 NIO2CH14:NIO 與 NIO2
CH14:NIO 與 NIO2
 

Viewers also liked

未来のプログラミング技術をUnityで -UniRx-
未来のプログラミング技術をUnityで -UniRx-未来のプログラミング技術をUnityで -UniRx-
未来のプログラミング技術をUnityで -UniRx-torisoup
 
Open Source Creativity
Open Source CreativityOpen Source Creativity
Open Source CreativitySara Cannon
 
The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...Brian Solis
 
Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)maditabalnco
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsBarry Feldman
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome EconomyHelge Tennø
 

Viewers also liked (7)

未来のプログラミング技術をUnityで -UniRx-
未来のプログラミング技術をUnityで -UniRx-未来のプログラミング技術をUnityで -UniRx-
未来のプログラミング技術をUnityで -UniRx-
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 
Open Source Creativity
Open Source CreativityOpen Source Creativity
Open Source Creativity
 
The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...The impact of innovation on travel and tourism industries (World Travel Marke...
The impact of innovation on travel and tourism industries (World Travel Marke...
 
Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)Reuters: Pictures of the Year 2016 (Part 2)
Reuters: Pictures of the Year 2016 (Part 2)
 
The Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post FormatsThe Six Highest Performing B2B Blog Post Formats
The Six Highest Performing B2B Blog Post Formats
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 

Similar to Linq 入门

Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
Java8 lambda
Java8 lambdaJava8 lambda
Java8 lambdakoji lin
 
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning ServicestwMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning ServicestwMVC
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學Bo-Yi Wu
 
Java 的開放原碼全文搜尋技術 - Lucene
Java 的開放原碼全文搜尋技術 - LuceneJava 的開放原碼全文搜尋技術 - Lucene
Java 的開放原碼全文搜尋技術 - Lucene建興 王
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdfczzz1
 
Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2terry28853669
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Justin Lin
 
高性能远程调用解决方案
高性能远程调用解决方案高性能远程调用解决方案
高性能远程调用解决方案Ady Liu
 
函数调用关系工具-2011-孙光福
函数调用关系工具-2011-孙光福函数调用关系工具-2011-孙光福
函数调用关系工具-2011-孙光福Wu Liang
 
chapter 1 basic knowledge about python.ppt
chapter 1 basic knowledge about python.pptchapter 1 basic knowledge about python.ppt
chapter 1 basic knowledge about python.pptqianruizhan
 
Erlang培训
Erlang培训Erlang培训
Erlang培训liu qiang
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introductionotakustay
 

Similar to Linq 入门 (20)

Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
Java8 lambda
Java8 lambdaJava8 lambda
Java8 lambda
 
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning ServicestwMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
 
Ooredis
OoredisOoredis
Ooredis
 
Ooredis
OoredisOoredis
Ooredis
 
PHP & MySQL 教學
PHP & MySQL 教學PHP & MySQL 教學
PHP & MySQL 教學
 
Java 的開放原碼全文搜尋技術 - Lucene
Java 的開放原碼全文搜尋技術 - LuceneJava 的開放原碼全文搜尋技術 - Lucene
Java 的開放原碼全文搜尋技術 - Lucene
 
functional-scala
functional-scalafunctional-scala
functional-scala
 
模块一-Go语言特性.pdf
模块一-Go语言特性.pdf模块一-Go语言特性.pdf
模块一-Go语言特性.pdf
 
Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2
 
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
Java SE 8 的 Lambda 連鎖效應 - 語法、風格與程式庫
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
高性能远程调用解决方案
高性能远程调用解决方案高性能远程调用解决方案
高性能远程调用解决方案
 
函数调用关系工具-2011-孙光福
函数调用关系工具-2011-孙光福函数调用关系工具-2011-孙光福
函数调用关系工具-2011-孙光福
 
chapter 1 basic knowledge about python.ppt
chapter 1 basic knowledge about python.pptchapter 1 basic knowledge about python.ppt
chapter 1 basic knowledge about python.ppt
 
Spark tutorial
Spark tutorialSpark tutorial
Spark tutorial
 
Clojure概览
Clojure概览Clojure概览
Clojure概览
 
Erlang培训
Erlang培训Erlang培训
Erlang培训
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introduction
 

Linq 入门