views:

96

answers:

2

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

For ex.: I have an User List with (Name, Email, PhoneNo,...) as properties. I will get the Users List(List<Users>) from database which is filled with all properties except Email. I need to update all the Email property in the list after retrieving from database with some email in session.

How can i do it?

+1  A: 

If I understood correctly, you want to retrieve all the users who have no email assigned. Then, what about something like this:

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

And if you want to update the users information back to the database:

dataContext.SubmitChanges();
Konamiman
+5  A: 

You should be able to do it simply via ForEach..

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

or for multiple properties..

users.ForEach(user => 
{
    user.email = sessionEmail;
    user.name = "Some Person";
    ...
});
Quintin Robinson
Prasad
Thats perfect. Thanks Quintin
Prasad