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<i...
LinqQuery
int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 };



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



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

 改一下:
  From p i...
复杂点的Linq
 按照字符长短,分类罗列出来

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

  ...
Code
string[] languages = { "Java", "C#", "C++", "Delphi",
"VB.net", "VC.net", "C++ Builder", "Kylix", "Perl",
"Python" };...
Linq用途

 主要是查找

  筛选

  排序

  聚合

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

   投影运算: Select, SelectMany

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

   数据分区: GroupBy

   限定符运算: ...
例子: 查字符串中的数字
string aString = "ABCDE99F-J74-12-89A";

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

var names2 = System.IO.File.ReadAllLines(@"names2.tx...
例子:单词是否都含r字母
string[] words = { "believe", "relief", "receipt",
"field" };

bool isWordsAllContainsRLetter = words.Any(w =...
Resharper
 VS2008 插件

 辅助编写代码

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




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



   bool Function(var i)
   {
     return i < 5;
   }  ...
参考资料

 LINQ 介绍

 101 LINQ Samples

 C# 中的 LINQ 入门
谢谢!
Upcoming SlideShare
Loading in...5
×

Linq 入门

1,647

Published on

简单介绍linq使用,方便从for、foreach转换到linq。

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,647
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Linq 入门

  1. 1. Linq 介绍 http://greenerycn.cnblogs.com 2010-10-19
  2. 2. 内容  从Foreach 到 Linq  Linq Example  Resharper插件
  3. 3. What is Linq  LINQ,语言级集成查询 (Language INtegrated Query)  仿照sql语言,实现在数组、list等可枚丼的数据集 中进行数据查找、排序  提高开发人员进行 查询信息的效率
  4. 4. Linq 今日主题
  5. 5. 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); } }
  6. 6. LinqQuery int[] arr = new int[] { 8, 5, 89, 3, 56, 4, 1, 58 }; var below5list = from i in arr where i < 5 select i;
  7. 7. 为啥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
  8. 8. 复杂点的Linq  按照字符长短,分类罗列出来 "Java","C#","C++","Delphi","VB.net","VC.net"," C++ Builder","Kylix","Perl","Python"  2 C#  3 C++  4 Java Perl  5: VB.net
  9. 9. 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;
  10. 10. Linq用途  主要是查找  筛选  排序  聚合  让代码文字化
  11. 11. 常用查询运算符  筛选数据: 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
  12. 12. 例子: 查字符串中的数字 string aString = "ABCDE99F-J74-12-89A"; var stringQuery = from ch in aString where Char.IsDigit(ch) select ch;
  13. 13. 例子:文本文件中的差异 var names1 = System.IO.File.ReadAllLines(@"names1.txt"); var names2 = System.IO.File.ReadAllLines(@"names2.txt"); // 在names1中但不再names2中的行 // 就是求 差集 var differenceQuery = names1.Except(names2);
  14. 14. 例子:单词是否都含r字母 string[] words = { "believe", "relief", "receipt", "field" }; bool isWordsAllContainsRLetter = words.Any(w => w.Contains("r"));
  15. 15. Resharper  VS2008 插件  辅助编写代码  提供普通for循环代码到linq代码的转换
  16. 16. ReSharper foreach (var i in arr) { if (i < 5) { below5list.Add(i); } } below5list.AddRange(arr.Where(i => i < 5))
  17. 17. Lambda表达式  arr.Where(i => i < 5) 匿名方法 bool Function(var i) { return i < 5; } 隐式声明
  18. 18. 参考资料  LINQ 介绍  101 LINQ Samples  C# 中的 LINQ 入门
  19. 19. 谢谢!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×