code_junkie

Update all objects in a collection using Linq

Nov 13th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. foreach (var c in collection)
  2. {
  3. c.PropertyToSet = value;
  4. }
  5.  
  6. collection.Select(c => {c.PropertyToSet = value; return c;}).ToList();
  7.  
  8. public static IEnumerable<T> ForEach<T>(
  9. this IEnumerable<T> source,
  10. Action<T> act)
  11. {
  12. foreach (T element in source) act(element);
  13. return source;
  14. }
  15.  
  16. collection.Iterate(c => { c.PropertyToSet = value;} );
  17.  
  18. public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T> callback)
  19. {
  20. if (enumerable == null)
  21. {
  22. throw new ArgumentNullException("enumerable");
  23. }
  24.  
  25. IterateHelper(enumerable, (x, i) => callback(x));
  26. }
  27.  
  28. public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
  29. {
  30. if (enumerable == null)
  31. {
  32. throw new ArgumentNullException("enumerable");
  33. }
  34.  
  35. IterateHelper(enumerable, callback);
  36. }
  37.  
  38. private static void IterateHelper<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
  39. {
  40. int count = 0;
  41. foreach (var cur in enumerable)
  42. {
  43. callback(cur, count);
  44. count++;
  45. }
  46. }
  47.  
  48. collection.Count(v => (v.PropertyToUpdate = newValue) == null);
  49.  
  50. public static class UpdateExtensions {
  51.  
  52. public delegate void Func<TArg0>(TArg0 element);
  53.  
  54. /// <summary>
  55. /// Executes an Update statement block on all elements in an IEnumerable<T> sequence.
  56. /// </summary>
  57. /// <typeparam name="TSource">The source element type.</typeparam>
  58. /// <param name="source">The source sequence.</param>
  59. /// <param name="update">The update statement to execute for each element.</param>
  60. /// <returns>The numer of records affected.</returns>
  61. public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> update)
  62. {
  63. if (source == null) throw new ArgumentNullException("source");
  64. if (update == null) throw new ArgumentNullException("update");
  65. if (typeof(TSource).IsValueType)
  66. throw new NotSupportedException("value type elements are not supported by update.");
  67.  
  68. int count = 0;
  69. foreach (TSource element in source)
  70. {
  71. update(element);
  72. count++;
  73. }
  74. return count;
  75. }
  76. }
  77.  
  78.  
  79.  
  80. int count = drawingObjects
  81. .Where(d => d.IsSelected && d.Color == Colors.Blue)
  82. .Update(e => { e.Color = Color.Red; e.Selected = false; } );
  83.  
  84. void DoStuff()
  85. {
  86. Func<string, Foo, bool> test = (y, x) => { x.Bar = y; return true; };
  87. List<Foo> mylist = new List<Foo>();
  88. var v = from x in mylist
  89. where test("value", x)
  90. select x;
  91. }
  92.  
  93. class Foo
  94. {
  95. string Bar { get; set; }
  96. }
  97.  
  98. Array.ForEach(MyCollection.ToArray(), item=>item.DoSomeStuff());
  99.  
  100. /// <summary>
  101. /// Executes an Update statement block on all elements in an IEnumerable of T
  102. /// sequence.
  103. /// </summary>
  104. /// <typeparam name="TSource">The source element type.</typeparam>
  105. /// <param name="source">The source sequence.</param>
  106. /// <param name="action">The action method to execute for each element.</param>
  107. /// <returns>The number of records affected.</returns>
  108. public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> action)
  109. {
  110. if (source == null) throw new ArgumentNullException("source");
  111. if (action == null) throw new ArgumentNullException("action");
  112. if (typeof (TSource).IsValueType)
  113. throw new NotSupportedException("value type elements are not supported by update.");
  114.  
  115. var count = 0;
  116. foreach (var element in source)
  117. {
  118. action(element);
  119. count++;
  120. }
  121. return count;
  122. }
Add Comment
Please, Sign In to add comment