Linq 入门
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Linq 入门

  • 1,883 views
Uploaded on

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

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

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,883
On Slideshare
1,676
From Embeds
207
Number of Embeds
1

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 207

http://www.cnblogs.com 207

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Linq 介绍 http://greenerycn.cnblogs.com 2010-10-19
  • 2. 内容  从Foreach 到 Linq  Linq Example  Resharper插件
  • 3. What is Linq  LINQ,语言级集成查询 (Language INtegrated Query)  仿照sql语言,实现在数组、list等可枚丼的数据集 中进行数据查找、排序  提高开发人员进行 查询信息的效率
  • 4. Linq 今日主题
  • 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. 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. 为啥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. 复杂点的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. 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. Linq用途  主要是查找  筛选  排序  聚合  让代码文字化
  • 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. 例子: 查字符串中的数字 string aString = "ABCDE99F-J74-12-89A"; var stringQuery = from ch in aString where Char.IsDigit(ch) select ch;
  • 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. 例子:单词是否都含r字母 string[] words = { "believe", "relief", "receipt", "field" }; bool isWordsAllContainsRLetter = words.Any(w => w.Contains("r"));
  • 15. Resharper  VS2008 插件  辅助编写代码  提供普通for循环代码到linq代码的转换
  • 16. ReSharper foreach (var i in arr) { if (i < 5) { below5list.Add(i); } } below5list.AddRange(arr.Where(i => i < 5))
  • 17. Lambda表达式  arr.Where(i => i < 5) 匿名方法 bool Function(var i) { return i < 5; } 隐式声明
  • 18. 参考资料  LINQ 介绍  101 LINQ Samples  C# 中的 LINQ 入门
  • 19. 谢谢!