how do you perform an iteration of a list over 2 lists in the context of the question body?

Because a value of each item in ListB needs to be set, you have no option but to iterate over that list.

To optimise the operation, you need to convert ListA into a structure that has O(1) access, to give an overall complexity of O(n), with n being the length of ListB.

As ListA is small, using an in-memory lookup shouldn’t be a problem:

var lookup = userIdNames.ToLookup(x => x.Id, x => x.Name);
foreach (var logItem in logList)
    logItem.UserName = lookup[logItem.UserId].FirstOrDefault();

This assumes that items in userIdNames with duplicate Id‘s always have the same Name.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top