Update all properties in the list using linq

I need to update all properties in a list object using linq.

For example: I have a list of users with names (Name, Email, PhoneNo, ...) as properties. I get a User List ( List<Users>) from the database, filled with all the properties except Email. I need to update all the email properties in the list after retrieving some email in the session from the database.

How can i do this?

+3
source share
2 answers

You can do it simply through ForEach..

users.ForEach(user => user.email = sessionEmail);

or for multiple properties.

users.ForEach(user => 
{
    user.email = sessionEmail;
    user.name = "Some Person";
    ...
});
+9
source

, , . , :

var users=dataContext.Users.Where(user => user.email==null).ToList();
foreach(var user in users) {
  user.Email="some@email.com"; //Or, choose a different email for each user
}

:

dataContext.SubmitChanges();
+1

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


All Articles