码迷,mamicode.com
首页 > Windows程序 > 详细

07.C#使用LINQ

时间:2015-03-05 16:43:02      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

1.LINQ基础

(1)LINQ的基本使用
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. int[] array = {1,3,5,7,9,2,4,6,8,0 };
  6. var linq = from i in array where i < 5 orderby i select i;
  7. foreach (int i in linq)
  8. {
  9. Console.WriteLine(i);//输出:0 1 2 3 4
  10. }
  11. }
  12. }
(2)LINQ查询的关键字
    基本关键字:frominwhereselect
    对结果排序的关键字:orderbyascendingdescending
    连接查询:joinonequals
    分组查询:groupby
    辅助关键字:intonew
(3)LINQ基本语法
    var result = from item in container
        [where(条件)]
        [orderby value [ascending|descending]]
        select item; 
    例如:
  1. class People
  2. {
  3. public string Name { set; get; }
  4. public int Age { set; get; }
  5. public override string ToString()
  6. {
  7. return Name + " " + Age;
  8. }
  9. }
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. People[] ps = new[] { new People { Name = "李志伟1", Age = 1 } ,
  15. new People { Name = "李志伟3", Age = 3 } ,
  16. new People { Name = "李志伟5", Age = 5 } ,
  17. new People { Name = "李志伟7", Age = 7 } ,
  18. new People { Name = "李志伟9", Age = 9 } ,
  19. new People { Name = "李志伟11", Age = 11 }
  20. };
  21. var linq = from p in ps where p.Age < 7 orderby p.Age descending select new People { Name =p.Name,Age=p.Age+10};
  22. foreach (People p in linq)
  23. {
  24. Console.WriteLine(p);//输出的Age分别是15 13 11
  25. }
  26. Console.Read();
  27. }
  28. }
(4)LINQ扩展函数(泛型函数)的使用
    用不同方式产生结果集: Reverse<>(), ToArray<>(), ToList<>() 
    集合操作: Distinct<>(), Union<>(), Intersect<>()
    统计函数: Count<>(), Sum<>(), Min<>(), Max<>()
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. int[] array = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
  6. var linq = from i in array where i > 5 orderby i select i;
  7. Console.WriteLine(linq.Count<int>());//输出8(array的元素个数)
  8. Console.WriteLine(linq.Max<int>());//输出9(array中最大的数)
  9. //输出8(array转换成List集合的元素个数)
  10. Console.WriteLine(linq.ToList<int>().Count);
  11. //输出4(array中除去重复的元素后元素个数)
  12. Console.WriteLine(linq.Distinct<int>().Count<int>());
  13. }
  14. }
(5)复杂的LINQ查询
    多个from子句
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. int[] array1 = { 1, 3, 5, 7, 9 };
  6. int[] array2 = { 0, 2, 4, 6, 8 };
  7. var linq = from i in array1 from j in array2 where i < 5 select i+j;
  8. foreach (int i in linq)
  9. {
  10. Console.WriteLine(i);//输出:1 3 5 7 9 3 5 7 9 11
  11. }
  12. }
  13. }
    使用join...on...equals连接查询
  1. class People
  2. {
  3. public string Name { set; get; }
  4. public int Age { set; get; }
  5. public override string ToString()
  6. {
  7. return Name + " " + Age;
  8. }
  9. }
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. People[] ps = new[] { new People { Name = "李志伟1", Age = 1 } ,
  15. new People { Name = "李志伟3", Age = 3 } ,
  16. new People { Name = "李志伟5", Age = 5 } ,
  17. new People { Name = "李志伟7", Age = 7 } ,
  18. new People { Name = "李志伟9", Age = 9 }
  19. };
  20. int[] array = { 1, 2, 3, 4, 5, 6 };
  21. var linq = from i in array join p in ps on i equals p.Age select p.Name;
  22. foreach (string name in linq)
  23. {
  24. Console.WriteLine(name);//输出:李志伟1 李志伟3 李志伟5
  25. }
  26. Console.Read();
  27. }
  28. }
    使用group...by...分组查询
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string[] words = { "apples", "blueberries", "oranges", "bananas", "apricots" };
  6. var wordGroups1 = from w in words group w by w[0];
  7. foreach (var item in wordGroups1)
  8. {
  9. Console.WriteLine(item.Key + "-->" + item.Count<string>());//a-->2 b-->2 o-->1
  10. }
  11. }
  12. }
    使用into的查询可以使用 into 上下文关键字创建一个临时标识符,以便将 group、join 或 select 子句的结果存储到新的标识符中。此标识符本身可以是附加查询命令的生成器。在 group 或 select 子句中使用新标识符的用法有时称为“延续”。
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string[] words = { "apples", "blueberries", "oranges", "bananas", "apricots" };
  6. var wordGroups1 = from w in words
  7. group w by w[0] into fruitGroup
  8. where fruitGroup.Count() >= 2
  9. select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };
  10. foreach (var item in wordGroups1)
  11. {
  12. Console.WriteLine("以{0}开头的有{1}个", item.FirstLetter, item.Words);
  13. }
  14. }
  15. }

2.LinqToXML

(1)LinqToXML类
技术分享
(2)



3.
4.
5.
未完,后续将会更新。。。
-------------------------------------------------------------------------------------------------------------------------------




07.C#使用LINQ

标签:

原文地址:http://www.cnblogs.com/LiZhiW/p/4315965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!