Asyncronous While Loop?

Posted by o7th Web Design on Stack Overflow See other posts from Stack Overflow or by o7th Web Design
Published on 2013-10-18T21:50:06Z Indexed on 2013/10/18 21:54 UTC
Read the original article Hit count: 127

Filed under:
|
|
|

I have a pretty great SqlDataReader wrapper in which I can map the output into a strongly typed list.

What I am finding now is that on larger datasets with larger numbers of columns, performance could probably be a bit better if I can optimize my mapping.

In thinking about this there is one section in particular that I am concerned about as it seems to be the heaviest hitter:

while (_Rdr.Read())
{
    T newObject = new T();
    for (int i = 0; i <= _Rdr.FieldCount - 1; ++i)
    {
        PropertyInfo info = (PropertyInfo)_ht[_Rdr.GetName(i).ToUpper()];
        if ((info != null) && info.CanWrite)
        {
            info.SetValue(newObject, (_Rdr.GetValue(i) is DBNull) ? default(T) : _Rdr.GetValue(i), null);
        }
    }
    _en.Add(newObject);
}
_Rdr.Close();

What I would really like to know, is if there is a way that I can make this loop asyncronous? I feel that will make all the difference in the world with this beast :)

Here is the entire Map method in case anyone can see where I can make further improvements on it...

IList<T> Map<T>

// Map our datareader object to a strongly typed list
private static IList<T> Map<T>(IDataReader _Rdr) where T : new() {
    try {
            Type _t = typeof(T);
            List<T> _en = new List<T>();
            Hashtable _ht = new Hashtable();
            PropertyInfo[] _props = _t.GetProperties();
            Parallel.ForEach(_props, info =>
            {
                _ht[info.Name.ToUpper()] = info;
            });
            while (_Rdr.Read())
            {
                T newObject = new T();
                for (int i = 0; i <= _Rdr.FieldCount - 1; ++i)
                {
                    PropertyInfo info = (PropertyInfo)_ht[_Rdr.GetName(i).ToUpper()];
                    if ((info != null) && info.CanWrite)
                    {
                        info.SetValue(newObject, (_Rdr.GetValue(i) is DBNull) ? default(T) : _Rdr.GetValue(i), null);
                    }
                }
                _en.Add(newObject);
            }
            _Rdr.Close();
            return _en;
    }catch(Exception ex){
        _Msg += "Wrapper.Map Exception: " + ex.Message;
        ErrorReporting.WriteEm.WriteItem(ex, "o7th.Class.Library.Data.Wrapper.Map", _Msg);
        return default(IList<T>);
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET