c# extension methods etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
c# extension methods etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

19 Temmuz 2013 Cuma

Extracting string between two string


 private static string ExtractName(this string src, string findfrom, string findto)
        {
            int start = src.IndexOf(findfrom);
            int to = src.IndexOf(findto, start + findfrom.Length);
            if (start < 0 || to < 0) return "";
            string s = src.Substring(
                           start + findfrom.Length,
                           to - start - findfrom.Length);
            return s;
        }

14 Haziran 2013 Cuma

Converting DataTable to List Object

source retrieved from stackoverflow

// your namespace{

  public static class DataTableExtensions
    {
        private static Dictionary<Type, IList<PropertyInfo>> typeDictionary = new Dictionary<Type, IList<PropertyInfo>>();
        public static IList<PropertyInfo> GetPropertiesForType<T>()
        {
                var type = typeof(T);
               
            if(!typeDictionary.ContainsKey(typeof(T)))
                {
                    typeDictionary.Add(type, type.GetProperties().ToList());
                }
                
            return typeDictionary[type];
        }

        /// <summary>
        /// usage  yourDataTable.ToList<YourType>()
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="table"></param>
        /// <returns>List<YourType></returns>
        public static IList<T> ToList<T>(this DataTable table) where T : new()
        {
            IList<PropertyInfo> properties = GetPropertiesForType<T>();
            IList<T> result = new List<T>();

            foreach (var row in table.Rows)
            {
                var item = CreateItemFromRow<T>((DataRow)row, properties);
                result.Add(item);
            }

            return result;
        }

        private static T CreateItemFromRow<T>(DataRow row, IList<PropertyInfo> properties) where T : new()
        {
            T item = new T();
            foreach (var property in properties)
            {
                property.SetValue(item, row[property.Name], null);
            }
            return item;
        }

    }

// }


checking data reader is null

using System;

public static class DataReaderExtensions
{
    public static bool IsDBNull(this IDataReader dataReader, string columnName)
    {
        return dataReader[columnName] == DBNull.Value;
    }
}

by this extension method you can check datareader column whether null or not

evren pehlivan yazılım

evren pehlivan yazılım, evren pehlivan yazılım geliştirici