DbCommand - ExecuteScalarToOrDefault
A DbCommand extension method that executes the scalar to or default operation.
public static void Main() { const string sql = @"SELECT 'FizzBuzz' As StringColumn"; using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())) { using (SqlCommand command = conn.CreateCommand()) { conn.Open(); command.CommandText = sql; //C# Extension Method: DbCommand - ExecuteScalarToOrDefault Console.WriteLine(command.ExecuteScalarToOrDefault<string>()); Console.WriteLine(command.ExecuteScalarToOrDefault<int>()); Console.WriteLine(command.ExecuteScalarToOrDefault(-1)); Console.WriteLine(command.ExecuteScalarToOrDefault(c => -2)); } } }
View Source
using System; using System.Data.Common; public static partial class Extensions { /// <summary> /// A DbCommand extension method that executes the scalar to or default operation. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <returns>A T.</returns> public static T ExecuteScalarToOrDefault<T>(this DbCommand @this) { try { return @this.ExecuteScalar().To<T>(); } catch (Exception) { return default(T); } } /// <summary> /// A DbCommand extension method that executes the scalar to or default operation. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="defaultValue">The default value.</param> /// <returns>A T.</returns> public static T ExecuteScalarToOrDefault<T>(this DbCommand @this, T defaultValue) { try { return @this.ExecuteScalar().To<T>(); } catch (Exception) { return defaultValue; } } /// <summary> /// A DbCommand extension method that executes the scalar to or default operation. /// </summary> /// <typeparam name="T">Generic type parameter.</typeparam> /// <param name="this">The @this to act on.</param> /// <param name="defaultValueFactory">The default value factory.</param> /// <returns>A T.</returns> public static T ExecuteScalarToOrDefault<T>(this DbCommand @this, Func<DbCommand, T> defaultValueFactory) { try { return @this.ExecuteScalar().To<T>(); } catch (Exception) { return defaultValueFactory(@this); } } }