Home > Software design >  How to merge two lists so that indexes remain the same?
How to merge two lists so that indexes remain the same?

Time:12-29

I have two lists, one for names, one for values. I need to merge them together so that first name in the list merges with first value from the other list. In other words, name and value need to have the same index in the same list.

I tried Concat() method, but it just expands the list by adding values to the end.

CodePudding user response:

It seems, that you are looking for Zip, not Concat:

List<string> names = ...
List<int> values = ...

var result = names
  .Zip(values, (name, value) => (name, value))
  .ToList(); // let's materialize result as a list
  • Related