一遍学习基础,一遍练习打字,很多乐趣。

代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6.  
  7. namespace dazilianxi
  8. {
  9. public class Category
  10. {
  11. public int Id { get;set;}
  12. public string Name { get; set; }
  13. }
  14. public class Product {
  15. public int Id { get; set; }
  16. public string Name { get; set; }
  17. public Category Category { get; set; }
  18. public DateTime BuyDate { get; set; }
  19. public decimal Price { get; set; }
  20.  
  21. public override string ToString()
  22. {
  23. return string.Format("编号:{0},名称:{1},类别:{2},购买时间:{3},价格{4}",Id,Name,Category.Name,BuyDate,Price);
  24.  
  25. }
  26. }
  27. public static class ProductHelper
  28. {
  29. public static IEnumerable<Product> GetProducts()
  30. {
  31. Category cat1 = new Category();
  32. cat1.Id = ;
  33. cat1.Name = "数码电子";
  34. Category cta2 = new Category();
  35. cta2.Id = ;
  36. cta2.Name = "服饰类";
  37.  
  38. List<Product> list = new List<Product>()
  39. {
  40. new Product(){ Id=,Name="数码相机",Price=1899M,BuyDate=new DateTime(,,),Category=cat1
  41. },
  42. new Product(){Id=,Name="录像机",Price=1000M,BuyDate=new DateTime(,,),Category=cat1},
  43. new Product(){Id=,Name="体恤衫",Price=150M,BuyDate=new DateTime(,,),Category=cta2},
  44. new Product(){Id=,Name="夹克衫",Price=180M,BuyDate=new DateTime(,,),Category=cta2}
  45. };
  46. return list;
  47. }
  48. }
  49.  
  50. public class ProductComparer : IComparer<Product>, IEqualityComparer<Product>//居然要具体的类
  51. {
  52. public int Compare(Product x, Product y)
  53. {
  54. if (x == y)//如果类别名称相同就比较产品价格
  55. {
  56. return x.Price.CompareTo(y.Price);
  57. }
  58. else //如果类别名称不同,比较类别的编号
  59. {
  60. return x.Category.Id.CompareTo(y.Category.Id);
  61. }
  62. }
  63.  
  64. public bool Equals(Product x, Product y)
  65. {
  66. if (x.Category.Name == y.Category.Name)
  67. {
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75.  
  76. //这个是接口里的,一定要实现
  77. public int GetHashCode(Product obj)
  78. {
  79. return obj.GetHashCode();
  80. }
  81. }
  82.  
  83. public class PropertyComparer<T> : IEqualityComparer<T>
  84. {
  85. //需要比较的属性的PropertyInfo
  86. private PropertyInfo _PropertyInfo;
  87.  
  88. //通过构造函数把需要比较的属性传进来
  89. public PropertyComparer(string propertyName)
  90. {
  91. _PropertyInfo = typeof(T).GetProperty(propertyName,BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);//原来这个地方也可以有几个选项| 神了
  92. if (_PropertyInfo == null)
  93. {
  94. throw new ArgumentException(string.Format("{0} 不是{1}的属性", propertyName, typeof(T)));
  95. }
  96. }
  97.  
  98. public bool Equals(T x, T y)
  99. {
  100. object xValue = _PropertyInfo.GetValue(x, null);
  101. object yValue = _PropertyInfo.GetValue(y, null);
  102.  
  103. //如果xValue的属性值为null,那yValue的属性值也必须是null,才返回true
  104. if (xValue == null)
  105. return yValue == null;
  106. return xValue.Equals(yValue);
  107. }
  108.  
  109. public int GetHashCode(T obj)
  110. {
  111. object propertyValue = _PropertyInfo.GetValue(obj, null);
  112. if (propertyValue == null)
  113. return ;
  114. else
  115. {
  116. return propertyValue.GetHashCode();
  117. }
  118. }
  119. }
  120. }
  1. var query = ProductHelper.GetProducts().OrderBy(x=>x.Id).OrderBy(b=>b.BuyDate).OrderByDescending(c=>c.Category.Id).ThenBy(p=>p.Price);
  2. showConsole(query);
  3. var query2 = ProductHelper.GetProducts().Select((x, index) => new { name=x.Name,index=index});//这里用到了一个匿名对象
  4. foreach(var item in query2){
  5. Console.WriteLine("名称:{0},索引{1}",item.name,item.index);
  6. }
  7. var query3 = ProductHelper.GetProducts().OrderBy(x => x, new ProductComparer());
  8. showConsole(query3);
  9. List<int> list = new List<int>()
  10. {
  11. , ,,,,,,,,,,
  12. };
  13. var query4 = list.Skip().Take();
  14. showConsole<int>(query4);
  15. Console.WriteLine("-------");
  16. var query5 = list.TakeWhile(x => x <= ).TakeWhile(x => x > );//
  17. showConsole(query5);
  18. Console.WriteLine("-------");
  19. var query6 = list.TakeWhile(x => x <= ).Where(x => x > );//3,2,3,4,5
  20. showConsole(query6);
  21. Console.WriteLine("-------");
  22. var query7 = list.TakeWhile(x => x <= ).Where(x => x > ).Distinct();//3,2,4,5 去重
  23. showConsole(query7);
  24. Console.WriteLine("-------");
  25. int[] arr1 = { , , , };
  26. int[] arr2 = { , , };
  27. var query8 = arr1.Intersect(arr2);//取交集2,3
  28. showConsole<int>(query8);
  29. Console.WriteLine("-------");
  30. var query9 = arr1.Except(arr2);//Except()获取第一个集合中有,而第二个集合中没有的元素 0,1
  31. showConsole<int>(query9);
  32. Console.WriteLine("-------");
  33. var query10 = arr1.Concat(arr2); //Concat()将2个集合串联起来,不剔除重复元素
  34. showConsole<int>(query10);
  35. Console.WriteLine("-------");
  36. var query11 = arr1.Union(arr2); //Union()将2个集合串联起来,剔除重复元素
  37. showConsole<int>(query11);
  38. Console.WriteLine("-------");
  39. //Zip()合并2个集合中位置相同的元素,将2个元素的操作结果返回一个新的元素。如果两个集合的长度不相等,以长度短的为准。
  40. int[] arr11 = { , };
  41. string[] arr22 = { "星期一", "星期二", "星期三" };
  42. var query12 = arr11.Zip(arr22, (x, y) => String.Format("{0},{1}", x, y));
  43. showConsole<string>(query12);
  44.  
  45. private static void showConsole<T>(IEnumerable<T> list)
  46. {
  47. foreach (T item in list)
  48. {
  49. Console.WriteLine(item.ToString());
  50. }
  51. }

总结:

● 一般性的条件筛选:Where() 
● 返回具体的集合类型再进行链式操作:OfType() 
● 非泛型集合转换成泛型集合后再使用LINQ操作符:Cast() 
● 排序、链式排序:OrderBy(), ThenBy(),实现IComparer<T>接口可以自定义排序规则 
● 投影:Select() 
● 返回前N个,跳过N个,分页:Take()和Skip()   
● 返回符合/不符合条件,但不执行完遍历:TakeWhile()和SkipWhile() 
● 反转集合元素:Reverse() 
● 空集合处理:DefaultIfEmpty() 
● 剔除集合中的重复元素:Distinct(),实现IEqualityComparer<Category>可自定义相等规则,针对某具体类或写一个泛型方法 
● 分组以及分组后投影:GroupBy()

● 2个集合的交集:Intersect() 
● 2个集合的查集:Except() 
● 2个集合的串联:Concat()和Union() 
● 2个集合的合并:Zip()

来源于:http://www.cnblogs.com/darrenji/p/3638561.html

http://www.cnblogs.com/darrenji/p/3638788.html

http://www.cnblogs.com/darrenji/p/3647823.html

非常基础的知识点,只有实践一次,才真的明白。

(C#基础)Linq学习理解的更多相关文章

  1. .Net程序员之Python基础教程学习----列表和元组 [First Day]

    一. 通用序列操作: 其实对于列表,元组 都属于序列化数据,可以通过下表来访问的.下面就来看看序列的基本操作吧. 1.1 索引: 序列中的所有元素的下标是从0开始递增的. 如果索引的长度的是N,那么所 ...

  2. (转)Linq学习笔记

    写在前面 最近在看Linq,在博客园看到这篇文章,写的通俗易懂,转来和大家一起做个分享.原文地址http://www.cnblogs.com/goscan/archive/2011/05/05/Lin ...

  3. C#之Linq学习笔记【转】

    写在前面 其实在09年就已经学习过Linq了,并被她那优美的语法所吸引,只是现在所在的公司还在使用VS2005在.Net2.0的框架下面的开发,所以Linq也很久没有用过了,最近看部门的同事对这个有些 ...

  4. 零基础如何学习java更有效呢?

    零基础学java,不知道该如何入手?也不知道学习的方向,很多人会问零基础怎么样学习,有没有什么入门的书籍推荐:只要方法正确,零基础学好java也是有机会的哦. 一.理解Java思想 Java是一门面向 ...

  5. LINQ to XML LINQ学习第一篇

    LINQ to XML LINQ学习第一篇 1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: public static void CreateDoc ...

  6. LINQ学习系列-----1.3 扩展方法

    这篇内容继续接着昨天的Lambda表达式的源码继续下去.昨天讲了Lambda表达式,此篇讲扩展方法,这两点都是Linq带来的新特性.    一.扩展方法介绍   废话不多说,先上源码截图: 上图中Ge ...

  7. face recognition[翻译][深度学习理解人脸]

    本文译自<Deep learning for understanding faces: Machines may be just as good, or better, than humans& ...

  8. 20145308 《网络对抗》Web安全基础实践 学习总结

    20145308 <网络对抗> Web安全基础实践 学习总结 实验内容 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 基础问题回答 (1)SQL注入攻击原理, ...

  9. 20145308 《网络对抗》 逆向及BOF基础实践 学习总结

    20145308 <网络对抗> 逆向及BOF基础实践 学习总结 实践目的 通过两种方法,实现程序能够运行原本并不会被运行的代码 实践原理 利用foo函数的Bof漏洞,构造一个攻击输入字符串 ...

随机推荐

  1. logistic回归和softmax回归

    logistic回归 在 logistic 回归中,我们的训练集由  个已标记的样本构成:.由于 logistic 回归是针对二分类问题的,因此类标记 . 假设函数(hypothesis functi ...

  2. Python3基础 str *运算 重复拼接字符串

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. JavaScript 装饰者模式(this运用)

    例: function ConcreteClass() { this.performTask = function () { this.preTask(); console.log('doing so ...

  4. 项目梳理6——使用WebApiTestClient为webapi添加测试

    1.使用nuget添加WebApiTestClient的引用 2.xxxxx.WebApi\Areas\HelpPage\Views\Help\Api.cshtml页面末尾添加如下代码: @Html. ...

  5. Elasticsearch工作原理

    一.关于搜索引擎 各位知道,搜索程序一般由索引链及搜索组件组成. 索引链功能的实现需要按照几个独立的步骤依次完成:检索原始内容.根据原始内容来创建对应的文档.对创建的文档进行索引. 搜索组件用于接收用 ...

  6. VS2010下创建WEBSERVICE,第二天 ----你会在C#的类库中添加web service引用吗?

    本文并不是什么高深的文章,只是VS2008应用中的一小部分,但小部分你不一定会,要不你试试: 本人对于分布式开发应用的并不多,这次正好有一个项目要应用web service,我的开发环境是vs2008 ...

  7. UVa 10905 孩子们的游戏

    https://vjudge.net/problem/UVA-10905 题意: 给定n个正整数,把它们连接成一个最大的整数. 思路: 实在是没想到直接用string来排序了. #include< ...

  8. UVa 12174 Shuffle(滑动窗口)

    https://vjudge.net/problem/UVA-12174 题意: 你在听音乐播放器,它采用随机播放形式.随机播放的原理时先随机产生一个1~n的排列,然后就按这个排列顺序播放歌曲.播放完 ...

  9. Java中创建只读容器,同步容器

    我们通过Collections.unmodifiableX来得到只读容器,因为容器被设为只读的,所以必须填入有意义的数据之后才进行设置 import java.util.ArrayList; impo ...

  10. Jenkins 对项目持续集成的配置之二 API接口自动化 Ant+Jmeter

    先介绍一下Ant+Jmeter 略 我的另一篇文章有讲在linux上部署ant + jmeter以满足CI持续化集成 https://www.cnblogs.com/qianjinyan/p/9067 ...