• c# Linq 及其扩展方法的使用


    1. public class LinqShow
    2. {
    3. ///
    4. /// Linq to object(数组、集合)--内存里面的数据
    5. /// Linq to sql(查询数据库用的)--在数据库数据
    6. /// Linq XML查询XML文件
    7. ///
    8. List studentsList = new List()
    9. {
    10. new Students
    11. {
    12. Id=1,
    13. Name="喜刷刷",
    14. ClassId=2,
    15. Age=22
    16. },
    17. new Students
    18. {
    19. Id=2,
    20. Name="喜刷刷2",
    21. ClassId=2,
    22. Age=22
    23. },
    24. new Students
    25. {
    26. Id=3,
    27. Name="喜刷刷3",
    28. ClassId=2,
    29. Age=22
    30. },
    31. new Students
    32. {
    33. Id=4,
    34. Name="喜刷刷4",
    35. ClassId=2,
    36. Age=22
    37. },
    38. new Students
    39. {
    40. Id=5,
    41. Name="喜刷刷3",
    42. ClassId=2,
    43. Age=22
    44. }
    45. };
    46. List stuClasses = new List
    47. {
    48. new StuClass
    49. {
    50. ID=1,
    51. Name="111"
    52. },
    53. new StuClass
    54. {
    55. ID=2,
    56. Name="222"
    57. },
    58. new StuClass
    59. {
    60. ID=3,
    61. Name="333"
    62. },
    63. new StuClass
    64. {
    65. ID=4,
    66. Name="444"
    67. }
    68. };
    69. public void Show()
    70. {
    71. List list = new List();
    72. #region
    73. foreach (var item in studentsList)
    74. {
    75. if (item.ClassId == 2)
    76. {
    77. list.Add(item);
    78. }
    79. }
    80. //自定义Linq
    81. list = AntWhere(studentsList, s => s.Age < 23);
    82. //官方Linq
    83. var list4 = studentsList.Where(s => s.Age < 23);
    84. //自定义扩展Linq
    85. var list7 = studentsList.AntWhere(s => s.Age < 23);
    86. //表达式方法(匿名类)将查询的结果一新的匿名类返回
    87. var list5 = from s in studentsList
    88. where s.Age == 22
    89. select new
    90. {
    91. Name = s.Name
    92. };
    93. var list6 = studentsList.Join(stuClasses, s => s.ClassId, c => c.ID, (s, c) => new
    94. {
    95. ID = c.ID,
    96. Name = c.Name
    97. });
    98. #endregion
    99. //EF框架简单使用
    100. OAEntities db = new OAEntities();
    101. var userInfoList = db.UserInfo.Where(s => s.ID > 1);
    102. foreach (var item in userInfoList)
    103. {
    104. Console.WriteLine(item.Remark);
    105. }
    106. }
    107. ///
    108. /// Func Students委托形参,bool委托返回值类型
    109. ///
    110. ///
    111. ///
    112. ///
    113. public ListAntWhere(List resource,Funcbool>where)
    114. {
    115. List list = new List();
    116. foreach (var item in resource)
    117. {
    118. if (where.Invoke(item))
    119. {
    120. list.Add(item);
    121. }
    122. }
    123. return list;
    124. }
    125. }
    126. ///
    127. /// 自定义扩展Linq方法
    128. ///
    129. public static class LinqExtend
    130. {
    131. public static IEnumerable<T> AntWhere<T>(this IEnumerable resource, Funcbool> where)
    132. {
    133. List list = new List();
    134. foreach (var item in resource)
    135. {
    136. if (where.Invoke(item))
    137. {
    138. list.Add(item);
    139. }
    140. }
    141. return list;
    142. }
    143. }

  • 相关阅读:
    Sidecar-详解 JuiceFS CSI Driver 新模式
    苹果审核:2.1性能完整性被拒解决
    13.5 GAS与连击
    C++11的内容介绍
    【PAT甲级 - C++题解】1049 Counting Ones
    解密list的底层奥秘
    Leetcode643:子数组最大平均数 I
    操作系统第三章习题及答案(汤子瀛第四版)
    掉干BeanUtil!把Bean自动映射了神器出Spring插件!
    彻底明白 js 中的 this 指向
  • 原文地址:https://blog.csdn.net/weixin_50784508/article/details/134537036