I have two collections of elements. I am trying to take all the elements in the first collection that have the same identifier in the second collection and run the CopyToDomain method against the corresponding elements.
The following code works fine, but I was a bit surprised at its verbosity. ReSharper does not recommend anything here, but I was wondering if it would be clearer to perform the intersection of two collections and then display the method on the elements? Would you make this change, or should I stop fussing and leave it as it is?
Task task = new Task(); IList<TaskAttributeDto> taskAttributeDtos = new List<TaskAttributeDto>(); taskAttributeDtos.Add(new TaskAttributeDto{ ID = 1}); taskAttributeDtos.Add(new TaskAttributeDto{ ID = 2}); foreach (TaskAttributeDto taskAttributeDto in taskAttributeDtos) { TaskAttribute matching = task.TaskAttributes.FirstOrDefault(t => t.ID == taskAttributeDto.ID); if (matching != null) { taskAttributeDto.CopyToDomain(matching); } }
source share