C# Datetable을 솔리드list 컬렉션 인스턴스로 변환

1368 단어 C#(ASP.Net) 포함
/// Datetable     list   
 public List ConvertDataTableToList(DataTable dt){
            //         
            List models = new List();

            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                Model model = new Model();
                //                 
                PropertyInfo[] propertys = model.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)//     Model      
                {
                    tempName = pi.Name;  //   Model      

                    if (dt.Columns.Contains(tempName)) //   DataTable         
                    {
                        //    Model          set      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];//  DataTable       
                        if (value != DBNull.Value)//             ,DBNull.Value,               ,DBNull.Value                ,        null 
                        {  
                            pi.SetValue(model, value, null);//SetValue   : https://blog.csdn.net/weixin_42263618/article/details/103748279
                        }

                    }
                }
                models.Add(model);
            }
            return models;
        }


    }

좋은 웹페이지 즐겨찾기