Convert DataTable to IEnumerable

前端 未结 8 1610
梦毁少年i
梦毁少年i 2020-11-29 22:50

I am trying to convert a DataTable to an IEnumerable. Where T is a custom type I created. I know I can do it by creating a List but I was thinking if t

8条回答
  •  情歌与酒
    2020-11-29 23:40

    If you want to convert any DataTable to a equivalent IEnumerable vector function.

    Please take a look at the following generic function, this may help your needs (you may need to include write cases for different datatypes based on your needs).

    /// 
        /// Get entities from DataTable
        /// 
        /// Type of entity
        /// DataTable
        /// 
        public IEnumerable GetEntities(DataTable dt)
        {
            if (dt == null)
            {
                return null;
            }
    
            List returnValue = new List();
            List typeProperties = new List();
    
            T typeInstance = Activator.CreateInstance();
    
            foreach (DataColumn column in dt.Columns)
            {
                var prop = typeInstance.GetType().GetProperty(column.ColumnName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                if (prop != null)
                {
                    typeProperties.Add(column.ColumnName);
                }
            }
    
            foreach (DataRow row in dt.Rows)
            {
                T entity = Activator.CreateInstance();
    
                foreach (var propertyName in typeProperties)
                {
    
                    if (row[propertyName] != DBNull.Value)
                    {
                        string str = row[propertyName].GetType().FullName;
    
                        if (entity.GetType().GetProperty(propertyName).PropertyType == typeof(System.String))
                        {
                            object Val = row[propertyName].ToString();
                            entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, Val, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
                        }
                        else if (entity.GetType().GetProperty(propertyName).PropertyType == typeof(System.Guid)) 
                        {
                            object Val = Guid.Parse(row[propertyName].ToString());
                            entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, Val, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
                        }
                        else
                        {
                            entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, row[propertyName], BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
                        }
                    }
                    else
                    {
                        entity.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(entity, null, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, null, null);
                    }
                }
    
                returnValue.Add(entity);
            }
    
            return returnValue.AsEnumerable();
        }
    

提交回复
热议问题