-5

i am trying to merge two list as below

Let list 1 contains id,name,age and list two contains int,varchar,varchar

My final out put must be id int,name varchar,age varchar

I am tried with out using linq or lamda expression

but i need this execution through it.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
GowthamanSS
  • 1,434
  • 4
  • 33
  • 58
  • 1
    What is the relationship between the two lists (i.e. what do the two lists join on)? Do they both have an `id` column or similar? – RB. Feb 14 '13 at 10:53
  • 1
    What type of List? List of string that contains those values? List of some custom entities? This question is very vague. – Leri Feb 14 '13 at 10:54
  • 1
    Your list contains varchar? – Sergey Berezovskiy Feb 14 '13 at 10:55
  • Why the linq is not an option ? Because it is ideal sulotion for your problem – VidasV Feb 14 '13 at 10:56
  • 3
    In fact, reading your question again, it sounds like you're trying to Union the two lists, not merge them. Please be *very* precise when editing your question to explain your requirements. – RB. Feb 14 '13 at 10:56
  • If you're very clear about your requirements, you can avoid the downvotes (and get better answers). Please improve your questions by using the ["edit"](http://stackoverflow.com/posts/14873129/edit) feature. – nawfal Feb 14 '13 at 18:05
  • @all here after i will thanks for your solution – GowthamanSS Feb 15 '13 at 05:19

2 Answers2

3

My answer assumes that both lists contain strings, that the number of items is the same in both lists and that they are already correctly ordered.

var result = list1.Select((x, i) => x + " " + list2[i]).ToList();

Alternatively, you can use Enumerable.Zip:

var result = list1.Zip(list2, (x, y) => x + " " + y).ToList();

Sample input:

var list1 = new List<string> { "id", "name", "age" };
var list2 = new List<string> { "int", "varchar", "varchar" };

Output:

var result = new List<string> { "id int", "name varchar", "age varchar" };
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
3

Use Zip, e.g. (to get matches as Tuples)

listOne.Zip(listTwo, Tuple.Create);
skolima
  • 31,963
  • 27
  • 115
  • 151