Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (var c in collection)
- {
- c.PropertyToSet = value;
- }
- collection.Select(c => {c.PropertyToSet = value; return c;}).ToList();
- public static IEnumerable<T> ForEach<T>(
- this IEnumerable<T> source,
- Action<T> act)
- {
- foreach (T element in source) act(element);
- return source;
- }
- collection.Iterate(c => { c.PropertyToSet = value;} );
- public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T> callback)
- {
- if (enumerable == null)
- {
- throw new ArgumentNullException("enumerable");
- }
- IterateHelper(enumerable, (x, i) => callback(x));
- }
- public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
- {
- if (enumerable == null)
- {
- throw new ArgumentNullException("enumerable");
- }
- IterateHelper(enumerable, callback);
- }
- private static void IterateHelper<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
- {
- int count = 0;
- foreach (var cur in enumerable)
- {
- callback(cur, count);
- count++;
- }
- }
- collection.Count(v => (v.PropertyToUpdate = newValue) == null);
- public static class UpdateExtensions {
- public delegate void Func<TArg0>(TArg0 element);
- /// <summary>
- /// Executes an Update statement block on all elements in an IEnumerable<T> sequence.
- /// </summary>
- /// <typeparam name="TSource">The source element type.</typeparam>
- /// <param name="source">The source sequence.</param>
- /// <param name="update">The update statement to execute for each element.</param>
- /// <returns>The numer of records affected.</returns>
- public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> update)
- {
- if (source == null) throw new ArgumentNullException("source");
- if (update == null) throw new ArgumentNullException("update");
- if (typeof(TSource).IsValueType)
- throw new NotSupportedException("value type elements are not supported by update.");
- int count = 0;
- foreach (TSource element in source)
- {
- update(element);
- count++;
- }
- return count;
- }
- }
- int count = drawingObjects
- .Where(d => d.IsSelected && d.Color == Colors.Blue)
- .Update(e => { e.Color = Color.Red; e.Selected = false; } );
- void DoStuff()
- {
- Func<string, Foo, bool> test = (y, x) => { x.Bar = y; return true; };
- List<Foo> mylist = new List<Foo>();
- var v = from x in mylist
- where test("value", x)
- select x;
- }
- class Foo
- {
- string Bar { get; set; }
- }
- Array.ForEach(MyCollection.ToArray(), item=>item.DoSomeStuff());
- /// <summary>
- /// Executes an Update statement block on all elements in an IEnumerable of T
- /// sequence.
- /// </summary>
- /// <typeparam name="TSource">The source element type.</typeparam>
- /// <param name="source">The source sequence.</param>
- /// <param name="action">The action method to execute for each element.</param>
- /// <returns>The number of records affected.</returns>
- public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> action)
- {
- if (source == null) throw new ArgumentNullException("source");
- if (action == null) throw new ArgumentNullException("action");
- if (typeof (TSource).IsValueType)
- throw new NotSupportedException("value type elements are not supported by update.");
- var count = 0;
- foreach (var element in source)
- {
- action(element);
- count++;
- }
- return count;
- }
Add Comment
Please, Sign In to add comment