/ICSharpCode.Decompiler/Tests/QueryExpressions.cs

https://github.com/siegfriedpammer/ILSpy
C# | 188 lines | 153 code | 18 blank | 17 comment | 2 complexity | 46b52b18a10768575eeb694f9c30c3f8 MD5 | raw file
  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. public class QueryExpressions
  22. {
  23. public class Customer
  24. {
  25. public int CustomerID;
  26. public IEnumerable<QueryExpressions.Order> Orders;
  27. public string Name;
  28. public string Country;
  29. public string City;
  30. }
  31. public class Order
  32. {
  33. public int OrderID;
  34. public DateTime OrderDate;
  35. public QueryExpressions.Customer Customer;
  36. public int CustomerID;
  37. public decimal Total;
  38. public IEnumerable<QueryExpressions.OrderDetail> Details;
  39. }
  40. public class OrderDetail
  41. {
  42. public decimal UnitPrice;
  43. public int Quantity;
  44. }
  45. public IEnumerable<QueryExpressions.Customer> customers;
  46. public IEnumerable<QueryExpressions.Order> orders;
  47. public object MultipleWhere()
  48. {
  49. return from c in this.customers
  50. where c.Orders.Count<QueryExpressions.Order>() > 10
  51. where c.Country == "DE"
  52. select c;
  53. }
  54. public object SelectManyFollowedBySelect()
  55. {
  56. return from c in this.customers
  57. from o in c.Orders
  58. select new
  59. {
  60. c.Name,
  61. o.OrderID,
  62. o.Total
  63. };
  64. }
  65. public object SelectManyFollowedByOrderBy()
  66. {
  67. return from c in this.customers
  68. from o in c.Orders
  69. orderby o.Total descending
  70. select new
  71. {
  72. c.Name,
  73. o.OrderID,
  74. o.Total
  75. };
  76. }
  77. public object MultipleSelectManyFollowedBySelect()
  78. {
  79. return from c in this.customers
  80. from o in c.Orders
  81. from d in o.Details
  82. select new
  83. {
  84. c.Name,
  85. o.OrderID,
  86. d.Quantity
  87. };
  88. }
  89. public object MultipleSelectManyFollowedByLet()
  90. {
  91. return from c in this.customers
  92. from o in c.Orders
  93. from d in o.Details
  94. let x = d.Quantity * d.UnitPrice
  95. select new
  96. {
  97. c.Name,
  98. o.OrderID,
  99. x
  100. };
  101. }
  102. public object FromLetWhereSelect()
  103. {
  104. return from o in this.orders
  105. let t = o.Details.Sum((QueryExpressions.OrderDetail d) => d.UnitPrice * d.Quantity)
  106. where t >= 1000m
  107. select new
  108. {
  109. OrderID = o.OrderID,
  110. Total = t
  111. };
  112. }
  113. public object MultipleLet()
  114. {
  115. return from a in this.customers
  116. let b = a.Country
  117. let c = a.Name
  118. select b + c;
  119. }
  120. public object Join()
  121. {
  122. return from c in this.customers
  123. join o in this.orders on c.CustomerID equals o.CustomerID
  124. select new
  125. {
  126. c.Name,
  127. o.OrderDate,
  128. o.Total
  129. };
  130. }
  131. public object JoinInto()
  132. {
  133. return from c in this.customers
  134. join o in this.orders on c.CustomerID equals o.CustomerID into co
  135. let n = co.Count<QueryExpressions.Order>()
  136. where n >= 10
  137. select new
  138. {
  139. Name = c.Name,
  140. OrderCount = n
  141. };
  142. }
  143. public object OrderBy()
  144. {
  145. return from o in this.orders
  146. orderby o.Customer.Name, o.Total descending
  147. select o;
  148. }
  149. public object GroupBy()
  150. {
  151. return from c in this.customers
  152. group c.Name by c.Country;
  153. }
  154. public object ExplicitType()
  155. {
  156. return from QueryExpressions.Customer c in this.customers
  157. where c.City == "London"
  158. select c;
  159. }
  160. public object QueryContinuation()
  161. {
  162. return from c in this.customers
  163. group c by c.Country into g
  164. select new
  165. {
  166. Country = g.Key,
  167. CustCount = g.Count<QueryExpressions.Customer>()
  168. };
  169. }
  170. }