Apply function to some elements of list

  • A+
Category:Languages

I have a list of complex objects i.e.

class MyObject  {     public bool selected;     public int id;     public string name; }  List<MyObject> theObjects = functionThatSelectsObjectsFromContainer(); 

And I have a list from another source that just give me int ids that are in the list of objects

List<int> idList = functionThatReturnsListOfIds(); 

Now for each of the items in the idList I want to set the selected property true. I know I can set up a foreach of one list and then search for the matching items in the other list and set it that way, but I was wondering if there's a different way that might be quicker.


I think you can do something like this, to make that working

foreach(var obj in theObjects.Where(o => idList.Any(i=> i == o.id))) {     obj.selected = true; } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: