列表集合循环和修改对象值

时间:2018-05-03 13:39:25

标签: c# list linq

语言c#,Visual Studio 2015

public class UserDTO
    {
        public string UserID {get;set;}
        public string UserName {get;set;}
        public string ManagerName {get;set;}
        public string ManagerEmail {get;set;}
    }

我得到List输入如下

List<UserDTO> lstUsr = new List<UserDTO>();
lstUsr.Add(new UserDTO { UserID = "A1", UserName = "AXZ" });
lstUsr.Add(new UserDTO { UserID = "B1", UserName = "BYZ" });

我有一个获取ManagerEmail和ManagerName的方法,输入是UserID

我需要循环lstUsr并更新同一列表中每个UserID的ManagerEmail和ManagerName。

现在我创建多个List并获得结果。我想知道它是否有效,可以使用相同的List吗?感谢pseduo代码帮助

string ShiftUserID = string.Join(",", lstUser.Distinct().Select(a => a.UserID));
private List<UserDTO> GetManagerDetail(string ShiftUserID )
        {
            List<UserDTO> lstMgrDetail = new List<UserDTO>();
            UserDTO oUser = null;
            string sMgrName;
            string sMgrMobile;
            string sMgrEmail;
            string[] sUserADID = sShiftUserADID.Split(',').Select(sValue => sValue.Trim()).ToArray();
            foreach (string sADID in sUserADID)
            {

                    oUser = new UserDTO();
                    ADHelperUtility.GetManagerDetail(ShiftUserID , out sMgrEmail, out sMgrName);
                    oUser.ManagerName = sMgrName;
                    oUser.ManagerEmail = sMgrEmail;
                    lstMgrDetail.Add(oUser);
                }

            return lstMgrDetail;
        }

1 个答案:

答案 0 :(得分:-1)

在您预期并设置值时迭代列表: 类似的东西:

foreach(UserDTO user in lstUsr)
{
   string managerName;
   string managerEmail = GetManagerInfoByID(user.UserID, out managerName); // Returns email and send out managerName. You could use an object here instead, just showing this for demo purposes.

  user.ManagerName = managerName;
  user.ManagerEmail = managerEmail;
}