IDataReader - ForEach
An IDataReader extension method that applies an operation to all items in this collection.
public static void Main() { string sql = "SELECT 'Fizz' UNION SELECT 'Buzz'"; string result = ""; using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())) { conn.Open(); using (DbCommand command = conn.CreateCommand()) { command.CommandText = sql; using (IDataReader reader = command.ExecuteReader()) { //C# Extension Method: IDataReader - ForEach reader.ForEach(dataReader => result += dataReader[0]); Console.WriteLine(result); } } } }
View Source
using System; using System.Data; public static partial class Extensions { /// <summary> /// An IDataReader extension method that applies an operation to all items in this collection. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="action">The action.</param> /// <returns>An IDataReader.</returns> public static IDataReader ForEach(this IDataReader @this, Action<IDataReader> action) { while (@this.Read()) { action(@this); } return @this; } }