Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# string
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 5-Dec-12 23:26pm
Edited 5-Dec-12 23:35pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1. using linq
http://www.functionx.com/csharp/linq/Lesson11.htm[^]
 
2. using 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!
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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(productCollection1.Count +
productCollection2.Count +
productCollection3.Count);
allProducts.AddRange(productCollection1);
allProducts.AddRange(productCollection2);
allProducts.AddRange(productCollection3);(AddRange is special-cased for ICollection for efficiency.)
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100