Jaa


Handy Extension Methods

This entry was edited July 16th, 2008 to include an additional extension method courtesy of Alex James

//Copyright (c) 2008 James Zimmerman //Permission is hereby granted, free of charge, to any person obtaining a copy //of this software and associated documentation files (the "Software"), to //deal in the Software without restriction, including without limitation the //rights to use, copy, modify, merge, publish, distribute, sublicense, and/or //sell copies of the Software, and to permit persons to whom the Software is //furnished to do so, subject to the following conditions: //The above copyright notice and this permission notice shall be included in //all copies or substantial portions of the Software. //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS //IN THE SOFTWARE. /// <summary> /// Performs a virtual paging databinding operation on a <see cref="DataGrid"/>. /// </summary> /// <param name="grid">The <see cref="DataGrid"/> to bind.</param> /// <param name="virtualCount">The <see cref="DataGrid.VirtualItemCount"/> value.</param> /// <param name="pageIndex">The <see cref="DataGrid.CurrentPageIndex"/> value.</param> /// <param name="dataSource">The <see cref="BaseDataList.DataSource"/> value.</param> internal static void DataBind(this DataGrid grid, Int32 virtualCount, Int32 pageIndex, Object dataSource) { grid.VirtualItemCount = virtualCount; grid.CurrentPageIndex = pageIndex - 1; grid.DataSource = dataSource; grid.DataBind(); } /// <summary> /// Extension method to get a value from an <see cref="IDataRecord"/> by name. /// </summary> public static DateTime GetDateTime(this IDataRecord record, String name) { return record.GetDateTime(record.GetOrdinal(name)); } /// <summary> /// Extension method to discover if a column is null in an <see cref="IDataRecord"/> by name. /// </summary> public static Boolean IsDBNull(this IDataRecord record, String name) { return record.IsDBNull(record.GetOrdinal(name)); } /// <summary> /// Extension method to get a value from an <see cref="IDataRecord"/> by name. /// </summary> public static Int32 GetInt32(this IDataRecord record, String name) { return record.GetInt32(record.GetOrdinal(name)); } /// <summary> /// Extension method to get a value from an <see cref="IDataRecord"/> by name. /// </summary> /// <remarks>This method will upcast any found <see cref="int"/> value to an <see cref="long"/></remarks> public static Int64 GetInt64(this IDataRecord record, String name) { Object result = record.GetValue(record.GetOrdinal(name)); return result is Int32 ? Convert.ToInt64(result) : (Int64) result; } /// <summary> /// Extension method to get a value from an <see cref="IDataRecord"/> by name. /// </summary> public static String GetString(this IDataRecord record, String name) { return record.GetString(record.GetOrdinal(name)); } /// <summary> /// Extension method to get a value from an <see cref="IDataRecord"/> by name. /// </summary> public static Decimal GetDecimal(this IDataRecord record, String name) { return record.GetDecimal(record.GetOrdinal(name)); } /// <summary> /// Extension method to get a value from an <see cref="IDataRecord"/> by name. /// </summary> public static Boolean GetBoolean(this IDataRecord record, String name) { return record.GetBoolean(record.GetOrdinal(name)); } /// <summary> /// Determines whether the specified column exists in the specified record. /// </summary> /// <param name="record">The <see cref="IDataRecord"/> to be evaluated.</param> /// <param name="name">The name of the column to search for.</param> /// <returns>True if the record contains the specified column; otherwise, false.</returns> public static bool ContainsColumn( this IDataRecord record, string name ) { for (Int32 i = 0; i < record.FieldCount; i++) { if (record.GetName(i).Equals(name, StringComparison.InvariantCultureIgnoreCase)) return true; } return false; } /// <summary> /// Extension method to convert an <see cref="IDataReader"/> to an implementation of <see cref="IEnumerable{T}"/> to enable linq support. /// </summary> /// <remarks>Special thanks to Alex James for this (https://www.base4.net/blog.aspx?ID=409)</remarks> /// <param name="reader">The <see cref="IDataReader"/> to be enumerated.</param> /// <returns>An <see cref="IEnumerable{IDataRecord}"/>.</returns> /// <example> /// <code> /// <![CDATA[ /// IEnumerable<FileInfo> files = from rec in reader.Enumerate() /// select new FileInfo(rec.GetString("FilePath")); /// ]]> /// </code> /// <code> /// <![CDATA[ /// const String SQL = "SELECT Firstname, Surname FROM Person"; /// IDataReader reader = ExecuteQuery(SQL); /// var e = from rec in reader.Enumerate() /// select new Person() /// { /// Firstname = rec.GetString("Firstname"), /// Surname = rec.GetString("Surname") /// }; /// IList<Person> people = new List<Person>(e); /// ]]> /// </code> /// </example> public static IEnumerable<IDataRecord> Enumerate(this IDataReader reader) { using (reader) { while (reader.Read()) { yield return reader; } } }


Jimmy Zimms is giving away free code again