Update List from another List with Matching values using Linq

Viewed 2353

I wish to update values in a List when I query context from another source

I can FIND the matches with 2 different styles of queries

var r = from aa in context.Persons.GetItems()
        join bb in findPersonViewModel.findPersonResultsViewModel on aa.PersonId equals bb.PersonID
        select aa;

OR

var results = context.Persons.GetItems().Where(ax => findPersonViewModel.findPersonResultsViewModel.Any(b => ax.PersonId == b.PersonID));

For Simplicity sake, I shortened the models

public class FindPersonResultsViewModel
{
    public int PersonID { get; set; }
    public bool ExistInContactManager { get; set; }
    public bool ActionType { get; set; }
}

public class PersonViewModel
{
     public int PersonID { get; set; }
}

Goal: If the PersonID matches, then update the FindPersonResultsViewModel to have both ExistinContactManager and ActionType to be True

Here is a sample of the data for a visual

var findPersonResultsViewModel = new List<FindPersonResultsViewModel>()
    { new FindPersonResultsViewModel { PersonID = 2, ActionType = false, ExistInContactManager = false },
      new FindPersonResultsViewModel { PersonID = 3, ActionType = false, ExistInContactManager = false },
      new FindPersonResultsViewModel { PersonID = 4, ActionType = false, ExistInContactManager = false },
      new FindPersonResultsViewModel { PersonID = 5, ActionType = false, ExistInContactManager = false },
      new FindPersonResultsViewModel { PersonID = 6, ActionType = false, ExistInContactManager = false },
    };



var personModel = new List<PersonViewModel>()
    { new PersonViewModel { PersonID = 2 },
      new PersonViewModel { PersonID = 6 },
      new PersonViewModel { PersonID = 8 },
      new PersonViewModel { PersonID = 9 },
      new PersonViewModel { PersonID = 12 },
      new PersonViewModel { PersonID = 22 },
      new PersonViewModel { PersonID = 32 },
      new PersonViewModel { PersonID = 42 },
    };
1 Answers

If I understand what you are asking... You could just use Contains and a foreach

var ids = personModel.Select(x => x.PersonID);

var results = findPersonResultsViewModel.Where(x => ids.Contains(x.PersonID));

foreach (var item in results)
{
   item.ActionType = true;       
   item.ExistInContactManager = true
}

db.SaveChanges();

Or you could save your self a round trip by just using a Join, and update using a foreach

var results =
   from pr in findPersonResultsViewModel
   join p in personModel on pr.PersonID equals p.PersonID
   select pr;

foreach (var item in results)
{
   item.ActionType = true;
   item.ExistInContactManager = true;
}

db.SaveChanges();

Output

PersonID : 2, ActionType :True, ExistInContactManager :True
PersonID : 3, ActionType :False, ExistInContactManager :False
PersonID : 4, ActionType :False, ExistInContactManager :False
PersonID : 5, ActionType :False, ExistInContactManager :False
PersonID : 6, ActionType :True, ExistInContactManager :True

Full Demo here

Related