fork(1) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public static class SimpleEnumerable
  6. {
  7. public static IEnumerable<TSource> SimpleWhere<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  8. {
  9. foreach (TSource element in source)
  10. {
  11. if (predicate(element))
  12. {
  13. yield return element;
  14. }
  15. }
  16. }
  17. }
  18.  
  19. public class MyClass
  20. {
  21. private string _att01;
  22. private string _att02;
  23. private string _att03;
  24. public int instanceNum;
  25.  
  26. public string att01
  27. {
  28. get
  29. {
  30. Console.WriteLine("att01 of instance {0}", instanceNum);
  31. return _att01;
  32. }
  33. set
  34. {
  35. _att01 = value;
  36. }
  37. }
  38.  
  39. public string att02
  40. {
  41. get
  42. {
  43. Console.WriteLine("att02 of instance {0}", instanceNum);
  44. return _att02;
  45. }
  46. set
  47. {
  48. _att02 = value;
  49. }
  50. }
  51.  
  52. public string att03
  53. {
  54. get
  55. {
  56. Console.WriteLine("att03 of instance {0}", instanceNum);
  57. return _att03;
  58. }
  59. set
  60. {
  61. _att03 = value;
  62. }
  63. }
  64. }
  65.  
  66. public class Test
  67. {
  68. public static void Main()
  69. {
  70. var coll = new[]
  71. {
  72. new MyClass { instanceNum = 0, att01 = "A", att02 = "B", att03 = "C" },
  73. new MyClass { instanceNum = 1, att01 = "X", att02 = "B", att03 = "C" },
  74. new MyClass { instanceNum = 2, att01 = "X", att02 = "Y", att03 = "C" },
  75. new MyClass { instanceNum = 3, att01 = "X", att02 = "Y", att03 = "Z" },
  76. };
  77.  
  78. Console.WriteLine("Method 1");
  79. var list1 = coll
  80. .Where(x => x.att01 == "X")
  81. .Where(x => x.att02 == "Y")
  82. .Where(x => x.att03 == "Z")
  83. .SingleOrDefault();
  84.  
  85. Console.WriteLine();
  86. Console.WriteLine("Method 2");
  87. var list2 = coll
  88. .Where(x => x.att01 == "X" && x.att02 == "Y" && x.att03 == "Z")
  89. .SingleOrDefault();
  90.  
  91. Console.WriteLine("Method 3");
  92. var list3 = coll
  93. .SimpleWhere(x => x.att01 == "X")
  94. .SimpleWhere(x => x.att02 == "Y")
  95. .SimpleWhere(x => x.att03 == "Z")
  96. .SingleOrDefault();
  97.  
  98. }
  99. }
Success #stdin #stdout 0.04s 24264KB
stdin
Standard input is empty
stdout
Method 1
att01 of instance 0
att01 of instance 1
att02 of instance 1
att01 of instance 2
att02 of instance 2
att03 of instance 2
att01 of instance 3
att02 of instance 3
att03 of instance 3

Method 2
att01 of instance 0
att01 of instance 1
att02 of instance 1
att01 of instance 2
att02 of instance 2
att03 of instance 2
att01 of instance 3
att02 of instance 3
att03 of instance 3
Method 3
att01 of instance 0
att01 of instance 1
att02 of instance 1
att01 of instance 2
att02 of instance 2
att03 of instance 2
att01 of instance 3
att02 of instance 3
att03 of instance 3