C# Convert DataTable to List<> Generic Collection

  /// <summary>
       /// Get a generic collection that reproduces multiple entity objects
       /// </summary>
       /// <returns>Generic collection</returns>
       public List<MODEL.ModelXtUserGroup> GetXtUserGroupModel()
       {
           List<MODEL.ModelXtUserGroup> list = null;
           StringBuilder sb = new StringBuilder("select SysId,GroupId,GroupName,CreateDate,CreateUser,LastModifyDate,LastModifyUser,DelFlag FROM XtUserGroup ");
           sb.Append("where SysId = 'MedicalApp' and DelFlag ='0'");
           DataTable dt = DBUtility.DbHelperSQL.QueryTable(sb.ToString());
           if (dt.Rows.Count > 0)
           {
               list = new List<MODEL.ModelXtUserGroup>();
               foreach (DataRow dr in dt.Rows)
               {
                   list.Add(DataRowToModel(dr));
               }
           }
           return list;
       }

 

  public MODEL.ModelXtUserGroup DataRowToModel(DataRow row)
       {
           MODEL.ModelXtUserGroup model = new MODEL.ModelXtUserGroup();
           if (row != null)
           {
               if (row["SysId"] != null)
               {
                   model.SysId = row["SysId"].ToString();
               }
               if (row["GroupId"] != null)
               {
                   model.GroupId = row["GroupId"].ToString();
               }
               if (row["GroupName"] != null)
               {
                   model.GroupName = row["GroupName"].ToString();
               }
               if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
               {
                   model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
               }
               if (row["CreateUser"] != null)
               {
                   model.CreateUser = row["CreateUser"].ToString();
               }
               if (row["LastModifyDate"] != null && row["LastModifyDate"].ToString() != "")
               {
                   model.LastModifyDate = DateTime.Parse(row["LastModifyDate"].ToString());
               }
               if (row["LastModifyUser"] != null)
               {
                   model.LastModifyUser = row["LastModifyUser"].ToString();
               }
               if (row["DelFlag"] != null)
               {
                   model.DelFlag = row["DelFlag"].ToString();
               }
           }
           return model;
       }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324999262&siteId=291194637
Recommended