日期:2014-05-18  浏览次数:20829 次

动态数组ADD和REMOVE不太明白。
以前人留下来的。有点不太懂。看看代码
C# code

 public string SyncUserInfor(HttpResponse Response)
    {
        GC.Collect();
        string jsBlock;
        StringBuilder sbLog = new StringBuilder();
        sbLog.Append(" 开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm")+ ";");
        
        DBOperator db = new DBOperator();
        //jsBlock = "<script>SetPorgressBar('开始读取数据库数据 ','0'); </script>";
        //Response.Write(jsBlock);
        //Response.Flush();
        int begin = Environment.TickCount;
        DataTable HRUserInfor = db.GetHRUserInfor();
        int end = Environment.TickCount;
        sbLog.Append(string.Format(" 获取数据库:{0}毫秒",end-begin));
        //jsBlock = "<script>SetPorgressBar('开始读域数据 ','0'); </script>";
        //Response.Write(jsBlock);
        //Response.Flush();
        begin = Environment.TickCount;
        ArrayList XMLUserInfor = xmlOperate.GetXMLInfor("0");
        end = Environment.TickCount;
        sbLog.Append(string.Format(" 获取域中所有信息:{0}毫秒", end - begin));
        ArrayList userProhibited = new ArrayList();
        ArrayList userModify = new ArrayList();
        ArrayList userModifyLog = new ArrayList();
        DataTable userAddTable = HRUserInfor;
        ArrayList MoveOUList = new ArrayList();
        DBOperator.SyncLog sl = new DBOperator.SyncLog();
        string UpdateNo = db.GetUpdateNo();
        int ModifyCount, AddCount, LeaveCount, totalCount, dataCount;
        totalCount = XMLUserInfor.Count;
        dataCount = HRUserInfor.Rows.Count;
        sl.UpdateNo = UpdateNo;
        if (XMLUserInfor != null)
        {
            for (int i = 0; i < XMLUserInfor.Count; i++)
            {
                userProhibited.Add((userInfor)XMLUserInfor[i]);
            }
            int count = XMLUserInfor.Count;
   
            for (int i = 0; i < count; i++)
            {

                
                   userInfor user = (userInfor)XMLUserInfor[i];
                DataRow[] drlist = HRUserInfor.Select(string.Format("samAccountName='{0}'", user.samAccountName));
                DataRow dr = null;
                if (drlist.Length < 1) continue;
                dr = drlist[0];
                userProhibited.Remove(user);
            }
            if (userProhibited.Count != 0)
            {
                
                for (int i = 0; i < userProhibited.Count; i++)
                {
                    userInfor user = (userInfor)userProhibited[i];
                    if (!user.samAccountName.EndsWith("_tmp"))
                    {
                        LeaveCount += 1;
                        op.ProhibitedUser(user.samAccountName);
                        DBOperator.SyncLog log = new DBOperator.SyncLog();
                        log.UpdateLevel = DBOperator.UpdateLevel.Hint;
                        log.UpdateType = DBOperator.UpdateType.Prohibit;
                        WriteLog(log, user);
                    }
                }
            }
        sbLog.Append("离职个数:" + LeaveCount + ";");
        }                




主要是userProhibited 这个动态数组。在FOR循环ADD。在后面一个FOR循环又REMOVE,之后不是userProhibited.COUNT = 0 吗?为什么后面又有个if (userProhibited.Count != 0) 。而且我调试后。后面这串确实有运行。难道userProhibited.Count != 0 这个是TRUE???? 求助。

------解决方案--------------------
Remove前是有条件的,if (drlist.Length < 1) continue;不满足就不会删除,他是要根据 XMLUserInfor去获取 drlist
------解决方案--------------------
userProhibited.Remove(user);不是每次循环都会执行的
DataRow[] drlist = HRUserInfor.Select(string.Format("samAccountName='{0}'", user.samAccountName));
的drlist>0的时候才会remove么,<1的话会continue.