How to execute lambda foreach expression on ObservableCollection <T>?

How to execute lambda foreach expression on ObservableCollection <T>?

There is no foreach method with ObservableCollection <T>, although this method exists with List <T>.

Is there any extension method?

+6
c # silverlight observablecollection
source share
3 answers

There is no default method in BCL, but directly it needs to write an extension method that has the same behavior (argument checking is omitted for brevity)

 public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) { foreach ( var cur in enumerable ) { action(cur); } } 

Use case

 ObservableCollection<Student> col = ...; col.ForEach(x => Console.WriteLine(x.Name)); 
+12
source share
 public static class EnumerableExtensions { public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) { foreach (var e in enumerable) { action(e); } } } 
+4
source share
 observableCollection.ToList().ForEach( item => /* do something */); 
0
source share

All Articles