Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge Two or more list according to order

Tags:

c#

I have two Lists

List<string> Name = new List<string>();
List<string> Address = new List<string>();

Both the lists having 30 data. I want to merge both the lists to get a complete Information lists like

List<string, string> CompleteInformation = new List<string, string>();

Also if I want to merge more than two list in one how it can be done.

like image 641
Pranab Avatar asked Dec 04 '22 07:12

Pranab


1 Answers

You're looking for Zip method:

var CompleteInformation = Name.Zip(Address, (n, a) => new { Address = a, Name = n }).ToList();

Gives you list of anonymous type instances, with two properties: Address i Name.

Update

You can call Zip more then once:

var CompleteInformation
    = Name.Zip(Address, (n, a) => new { Address = a, Name = n })
          .Zip(AnotherList, (x, s) => new { x.Address, x.Name, Another = s })
          .ToList();
like image 115
MarcinJuraszek Avatar answered Dec 22 '22 09:12

MarcinJuraszek