For example I have these two lists:
List<string> firstName = new List<string>();
List<string> lastName = new List<string>();
How can I concatenate firstName[0] and lastName[0], firstName[1] and lastName[1], etc and put it in a new list?
For example I have these two lists:
List<string> firstName = new List<string>();
List<string> lastName = new List<string>();
How can I concatenate firstName[0] and lastName[0], firstName[1] and lastName[1], etc and put it in a new list?
It sounds like you want Zip
from LINQ:
var names = firstName.Zip(lastName, (first, last) => first + " " + last)
.ToList();
(This isn't really concatenating the two lists. It's combining them element-wise, but that's not really the same thing.)
EDIT: If you're using .NET 3.5, you can include the Zip
method yourself - Eric Lippert's blog post on it has sample code.
ToList
call. –
Featly You can Use Linq Concat and ToList methods
var lists = firstname.Concat(lastname).ToList();
© 2022 - 2024 — McMap. All rights reserved.