简体   繁体   English

如何在C#的列表中存储多个项目

[英]how to store multiple items in a list in c#

I am getting a list of users details and storing some of details into another list and returning that list, but the problem is at the end it is returning correct number of rows but each row has same values that is the last row's values 我正在获取用户详细信息列表,并将一些详细信息存储到另一个列表中并返回该列表,但问题是最后返回的是正确的行数,但每行的值与最后一行的值相同

GetUserList usr = new GetUserList();
List<GetUserList> UsersList = new List<GetUserList>();
List<users> lstusers= usersBase.GetUsersListByOrgId(OrgId, null);

try
{
    if (lstusers.Count > 0)
    {
        usr.Message = "Success";

        for (int i = 0; i < lstusers.Count; i++)
        {
            usr.UserID = Convert.ToInt32(lstusers[i].userID);
            usr.UserFirstName = Convert.ToString(lstusers[i].forename);
            UsersList.Add(usr);

            //return usr;
        }
    }
    else
    {
        usr.Message = "Failed";
    }

    return UsersList;
}
catch (Exception e)
{
    throw e;
}

Just move declaration/instantiation of usr into the loop: 只需将usr声明/实例化进入循环即可:

for (int i = 0; i < lstusers.Count; i++)
{
    GetUserList usr = new GetUserList();
    usr.UserID = Convert.ToInt32(lstusers[i].userID);
    usr.UserFirstName = Convert.ToString(lstusers[i].forename);
    UsersList.Add(usr);

    //return usr;
}

otherwise you're adding to the list reference to the same instance of GetUserList all the time and overwriting its values - so you end up with each row has same values that is the last row's values . 否则,您将一直添加到对GetUserList的相同实例的列表引用,并覆盖其值-因此最终,每行的值与最后一行的值相同。

You have to instantiate object inside for loop, currently same object is stored in list multiple time, change your code like: 您必须在for循环中实例化对象,当前同一对象多次存储在列表中,请更改您的代码,例如:

for (int i = 0; i < lstusers.Count; i++)
{
   GetUserList usr = new GetUserList(); // not this line
   usr.Message = "Success";
   usr.UserID = Convert.ToInt32(lstusers[i].userID);
   usr.UserFirstName = Convert.ToString(lstusers[i].forename);
   UsersList.Add(usr);


}

Currently you are modifying the same object every time, so the result would be the last updated content in all list items. 当前,您每次都在修改同一对象,因此结果将是所有列表项中的最后更新内容。

because you are updating same user every time in loop iteration and adding again and again into the list. 因为您每次循环迭代时都要更新同一用户,并一次又一次地将其添加到列表中。 you should create a new user every time in loop iteration 您应该在每次循环迭代时创建一个新用户

GetUserList usr ;
List<GetUserList> UsersList = new List<GetUserList>();
List<users> lstusers= usersBase.GetUsersListByOrgId(OrgId, null);



try
{
    if (lstusers.Count > 0)
    {


        for (int i = 0; i < lstusers.Count; i++)
        {
            //create a new instance of user here
            usr = new GetUserList();
            usr.Message = "Success";
            usr.UserID = Convert.ToInt32(lstusers[i].userID);
            usr.UserFirstName = Convert.ToString(lstusers[i].forename);
            UsersList.Add(usr);


            //return usr;
        }
    }
    else
    {
        usr.Message = "Failed";
    }


    return UsersList;
}

catch (Exception e)
{
    throw e;
}

Use new User object every time 每次使用新的User对象

GetUserList usr =null;
    List<GetUserList> UsersList = new List<GetUserList>();
    List<users> lstusers= usersBase.GetUsersListByOrgId(OrgId, null);



    try
    {
        if (lstusers.Count > 0)
        {
           // ;

            for (int i = 0; i < lstusers.Count; i++)
            {
                usr= new GetUserList();
                usr.Message = "Success"  
                usr.UserID = Convert.ToInt32(lstusers[i].userID);
                usr.UserFirstName = Convert.ToString(lstusers[i].forename);
                UsersList.Add(usr);


                //return usr;
            }
        }
        else
        {
            usr.Message = "Failed";
        }


        return UsersList;
    }

    catch (Exception e)
    {
        throw e;
    }

You are adding the same instance of GetUserList into the list. 您正在将GetUserList的相同实例添加到列表中。 In the loop you update the fields usr.UserID usr.UserFirstName for only one instance of GetUserList class. 在循环中,仅为GetUserList类的一个实例更新字段usr.UserID usr.UserFirstName。 So the effect is (lets say if lstusers.Count = 10), that you add 10 time this object to the list. 因此,效果是(假设lstusers.Count = 10)是您将此对象添加10倍到列表中。 Move GetUserList usr = new GetUserList(); 移动GetUserList usr = new GetUserList(); inside the loop and usr.Message = as well. 在循环内,并且usr.Message =也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM