Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I have 1 list of records that list have some sub list of records i need merge that two list record in generic list .


Any one help me ....



EXAMPLE

LIST 1     A1,A2,A3,A4                LIST 2 B1,B2,B3,B4

Need output like that
A1 B1
A1 B2
A1 B3
A1 B4
A2 b1
A2 B2
.
.
.



like that i need 



What I have tried:

C#
foreach (GroupRouting objGroupRouting in objlstGroupRouting)
                    {
                        foreach (OutwardRouting objOutwardRouting in objGroupRouting.Outward)
                        { 
                            objlstOutwardRouting.Add(objOutwardRouting);
                        }
                    }
                    objlstOutwardRouting = objlstOutwardRouting.Union(objlstOutwardRouting).ToList();
Posted
Updated 3-Mar-16 10:37am
v3
Comments
Andy Lanng 29-Feb-16 8:42am    
whats the problem
satheeshkumar chinnadurai 29-Feb-16 9:06am    
I AM NOT GETTING

Need output like that
A1 B1
A1 B2
A1 B3
A1 B4
A2 b1
A2 B2

CAN YOU PLEASE HELP ME...
BulletVictim 29-Feb-16 9:34am    
having to lists with only one input is going to be difficult to join as there is no common field in the two lists. So at least one list will need to have more than one value contained per object.
ZurdoDev 29-Feb-16 9:40am    
A nested loop is one way to go so why doesn't your code work? Don't union them.

Loop through list 1, which will go A1, A2, etc. And then in inner loop, go through list 2, B1, B2, etc and add to the looping var from the first loop.

1 solution

Seems you want to perform cross join operation.
Check this:

C#
List<string> l1 = new List<string>(){"A1", "A2", "A3", "A4"};
List<string> l2 = new List<string>(){"B1", "B2", "B3", "B4"};

//standard query
//var allitems = from a in l1 
//				from b in l2
//				select new {
//					A = a,
//					B = b
//				};

//using lambda expressions
var lambdaitems = l1.SelectMany(x=> l2, (x, y) => new {A = x, B = y});


Both queries return the same result:

A B
A1 B1 
A1 B2 
A1 B3 
A1 B4 
A2 B1 
A2 B2 
A2 B3 
A2 B4 
A3 B1 
A3 B2 
A3 B3 
A3 B4 
A4 B1 
A4 B2 
A4 B3 
A4 B4 


Try!

For further information, please see:
101 LINQ Samples in C#[^]
LINQ 101 Samples - Lambda Style[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900