Click here to Skip to main content
15,886,791 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.
Is there any fast and good method to combine all strings in x-list in any possible way?
I have List<List<string>>, which looks like:
0: A
B
C

1: 1
2
3

2: X
Y
Z

And my output should be like:
A1X, A1Y, A1Z, A2X, A2Y, A2Z, A3X, A3Y, A3Z, B1X, B2Y etc.

Any idea how can i do that?

[edit]Fixed HTML on the datatype - OriginalGriff[/edit]
Posted
Updated 5-Dec-12 22:35pm
v2

1. using linq
http://www.functionx.com/csharp/linq/Lesson11.htm[^]

2. using sql
SQL
select distinct list1.a + list2.a + list3.a as Combination from
(   select 'a' as a union all   select 'b' as a union all   select 'c' as a) as list1
cross join
(   select '1' as a union all   select '2' as a union all   select '3' as a ) as list2
cross join
(   select 'x' as a union all   select 'y' as a union all   select 'z' as a) as list3


3. Many For loops

Happy Coding!
:)
 
Share this answer
 
Hello Trucha ji
You can use LINQ CONCAT & TOLIST

var allProducts = productCollection1.Concat(productCollection2)
.Concat(productCollection3)
.ToList();Note that there are more efficient ways to do this - the above will basically loop through all the entries, creating a dynamically sized buffer. As you can predict the size to start with, you don't need this dynamic sizing... so you could use:

var allProducts = new List<product>(productCollection1.Count +
productCollection2.Count +
productCollection3.Count);
allProducts.AddRange(productCollection1);
allProducts.AddRange(productCollection2);
allProducts.AddRange(productCollection3);(AddRange is special-cased for ICollection<t> for efficiency.)
 
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