I have two IEnumerable objects called items
and newItems
. I need to update the Did
and KeyDid
property of each element in items
with the matching Did
and KeyDid
from newItems
. I can't just call items = newItems;
since other things have a reference to items
. Here's my code:
T[] arrItems = items as T[] ?? items.ToArray();
if (arrItems.Length != newItems.Count())
throw new Exception("Item counts do not match.");
//Copy Did and KeyDid from old items to newItems
int i = 0;
foreach (T newItem in newItems)
{
T oldItem = arrItems[i++];
oldItem.Did = newItem.Did;
oldItem.KeyDid = newItem.KeyDid;
}
I'm interested in a more concise way to do this, perhaps using a single LINQ expression. Any ideas how to improve this code in terms of readability, clearness, conciseness, or overall coolness?
.Count()
call should be moved to after the.ToArray()
- I'll update the code. \$\endgroup\$