locked
Help needed in static method RRS feed

  • Question

  • User1489758560 posted

    Hi,

    How can i make this static method as async. Basically this method will map the data reader property with class property.

    public static List<T> ConvertList<T>(this DbDataReader dr) where T : new()
            {
                List<T> lstRet= null;
                var Entity = typeof(T);
                var PropDict = new Dictionary<string, PropertyInfo>();
                try
                {
                    if (dr != null && dr.HasRows)
                    {
                        RetVal = new List<T>();
                        var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                        while (dr.Read())
                        {
                            T newObject = new T();
                            for (int Index = 0; Index < dr.FieldCount; Index++)
                            {
                                if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                                {
                                    var Info = PropDict[dr.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = dr.GetValue(Index);
                                        Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                    }
                                }
                            }
                            lstRet.Add(newObject);
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return lstRet;
            }

    i tried to add async key word in front of the List<T>. i but i am not sure where to use the await in the code. any help will be highly appreciated

    Monday, February 17, 2020 5:05 PM

Answers

  • User-474980206 posted

    you need to do an async read:

    public static async Task<List<T>> ConvertList<T>(this DbDataReader dr) where T : new()
            {
                List<T> lstRet= null;
                var Entity = typeof(T);
                var PropDict = new Dictionary<string, PropertyInfo>();
                try
                {
                    if (dr != null && dr.HasRows)
                    {
                        RetVal = new List<T>();
                        var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                        while (await dr.ReadAsync())
                        {
                            T newObject = new T();
                            for (int Index = 0; Index < dr.FieldCount; Index++)
                            {
                                if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                                {
                                    var Info = PropDict[dr.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = dr.GetValue(Index);
                                        Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                    }
                                }
                            }
                            lstRet.Add(newObject);
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return lstRet;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 17, 2020 5:40 PM

All replies

  • User-474980206 posted

    you need to do an async read:

    public static async Task<List<T>> ConvertList<T>(this DbDataReader dr) where T : new()
            {
                List<T> lstRet= null;
                var Entity = typeof(T);
                var PropDict = new Dictionary<string, PropertyInfo>();
                try
                {
                    if (dr != null && dr.HasRows)
                    {
                        RetVal = new List<T>();
                        var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                        PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                        while (await dr.ReadAsync())
                        {
                            T newObject = new T();
                            for (int Index = 0; Index < dr.FieldCount; Index++)
                            {
                                if (PropDict.ContainsKey(dr.GetName(Index).ToUpper()))
                                {
                                    var Info = PropDict[dr.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = dr.GetValue(Index);
                                        Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                    }
                                }
                            }
                            lstRet.Add(newObject);
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return lstRet;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 17, 2020 5:40 PM
  • User1489758560 posted

    Hi Bruce,

    Thanks a lot. 

    Monday, February 17, 2020 5:47 PM