Refresh item in list not working

I tried to simplify my problem in this example:

List<User> userList = new List<User>()
                {
                    new User {IdUser = 1, Login = "Frank" },
                    new User {IdUser = 2, Login = "Pat" },
                    new User {IdUser = 3, Login = "Max" },
                    new User {IdUser = 4, Login = "Paul" },
                    new User {IdUser = 5, Login = "John" }
                };

User newUser = new User()
    {
         IdUser = 3,
         Login = "Chris"
    };

var userToUpdate = userList.FirstOrDefault(r => r.IdUser == 3);
userToUpdate = newUser;

why userListdoesn't it matter Login = "Chris"at the end of this program? How can I update an item in a list?

PS1: I do not want to update the value Login, I want to update the User object

PS2: he says in this link that FirstOrDefault selects an item in the collection but does not "debug" or "clone" it. That is, this is the same instance. So, if you change a property, you change the original instance.

I'm confused!

+4
source share
6 answers

Performing this action:

var userToUpdate = userList.FirstOrDefault(r => r.IdUser == 3);

, . , , : userToUpdate userList.

newUser to userToUpdate , , userToUpdate, newUser.

, - :

var index = userList.FindIndex(r => r.IdUser == 3);

if (index != -1)
{
    userList[index].Id = newUser.Id,
    //set all other properties from newUser
}

:

var index = userList.FindIndex(r => r.IdUser == 3);

if (index != -1)
{
    userList[index] = newUser;        
}
+5

, . User , .

:

var userToUpdate = userList.FirstOrDefault(r => r.IdUser == 3);

, . , newUser: userToUpdate = newUser;. , IdUser 3 , . :

int toReplaceIndex = userList.FindIndex(r => r.IdUser == 3);

if (toReplaceIndex == -1)
{
    userList[toReplaceIndex] = newUser;
}
else
{
    // do whatever you want when the user ID being replaced doesn't exist.
}
+3

, .

:

var ind = userList.IndexOf(userToUpdate);
userList[ind] = newUser;
+1

var userToUpdate = userList.FirstOrDefault(r => r.IdUser == 3);

, .

0

, , , . :

List<User> userList = new List<User>()
    {
        new User {IdUser = 1, Login = "Frank" },
        new User {IdUser = 2, Login = "Pat" },
        new User {IdUser = 3, Login = "Max" },
        new User {IdUser = 4, Login = "Paul" },
        new User {IdUser = 5, Login = "John" }
    };

User newUser = new User()
{
    IdUser = 3,
    Login = "Chris"
};

var foundUser = userList.FirstOrDefault(item => item.IdUser == newUser.IdUser);
if(foundUser != null)
    userList.Remove(foundUser)
userList.Add(newUser);
0

:

, , - 1. , , .FirstOrDefault, " " . , , , , , . ( , ).

, userToUpdate - 1 , , . (. 2), - (newUser).

What do you do next? Return to the previous page and replace the information written there (on page 1) from page 2 containing the entry: "the object is in the dining room."

Then you break away from the page and pass it on to the “other guy,” saying “Yo! I need some changes for object1, here is paper with an address, this will help you find, go and change it.”

Guess what happens next? He will go to the dining room.

0
source

Source: https://habr.com/ru/post/1668537/


All Articles