Click here to Skip to main content
11,412,834 members (74,210 online)
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 OriginalGriff 404
1 Sascha Lefévre 200
2 Maciej Los 150
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,317
2 Maciej Los 3,570
3 Abhinav S 3,298
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web03 | 2.8.150427.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2015
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