Better use Dictionary<TK,TV> :
Dictionary<int,item> l2dic = List2.ToDictionary(x => x.identifier); item itm; List1.ForEach(x => { if(l2dic.TryGetValue(x.identifier,out itm)) { x.name = itm.name; } });
Or, as @Rawling says, use the foreach instead:
Dictionary<int,item> l2dic = List2.ToDictionary(x => x.identifier); item itm; foreach(item x in List1) { if(l2dic.TryGetValue(x.identifier,out itm)) { x.name = itm.name; } }
An ideal demonstration (with minor changes to the item class).
This runs on average linear time, while your approach works in quadratic time.
Supposed that the identifiers are unique: none of the two elements in the same list can have the same identifier.
The final note is that variables generally begin with a lowercase list1 , so list1 and list2 , while classes and properties begin with capital (these are item , Identifier and Name ).
source share