ShaneKm
ShaneKm

Reputation: 21368

Best way to enumerate all combinations

Suppose I have the following list in any order:

S1 
R1 
R2 
S2
S3

I need to create a LIST where each "S" contains each "R" combination

Output:

S1 => R1
S1 => R2
S2 => R1
S2 => R2
S3 => R1
S3 => R2

what's the best way to achieve this? thanks

Upvotes: 0

Views: 139

Answers (4)

lukiller
lukiller

Reputation: 1207

If this is always static, I mean always two lists (or prefixes), you can do nested loops:

List<string> list = new List<string>();
list.Add("S1");
list.Add("R1");
list.Add("R2");
list.Add("S2");
list.Add("S3");

loop

foreach (var s in list.Where(l => l.StartsWith("S")))
{
    foreach (var r in list.Where(l => l.StartsWith("R")))
    {
        Console.WriteLine(string.Format("{0} => {1}", s, r));
    }
}

or:

list.Where(l => l.StartsWith("S"))
    .ToList()
    .ForEach(s => list.Where(l => l.StartsWith("R"))
                      .ToList()
                      .ForEach(r => Console.WriteLine(string.Format("{0} => {1}", s, r)))
            );

Instead of Console.WriteLine, you can add the result to another list.

Upvotes: 0

Jacob
Jacob

Reputation: 78920

Try:

var byPrefix = list.GroupBy(i => i.First()).ToDictionary(g => g.Key, g => g);
var result = 
    from s in byPrefix['S']
    from r in byPrefix['R']
    select new { s, r };

Upvotes: 5

fsacer
fsacer

Reputation: 1402

This is what I came up with, a mixture of array and enumerators:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = new string[] {
                "S1", 
                "R1", 
                "R2", 
                "S2",
                "S3"
            };
            foreach (var item in CombinationsOfSAndR(input)) 
            {
                Console.WriteLine("{0} -> {1}", item.Key, item.Value);
            }
            /* OUT:
            S1 -> R1
            S1 -> R2
            S2 -> R1
            S2 -> R2
            S3 -> R1
            S3 -> R2 */
        }
        static IEnumerable<KeyValuePair<string, string>> CombinationsOfSAndR(string[] input)
        {
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i].StartsWith("S"))
                {
                    for (int j = 0; j < input.Length; j++)
                    {
                        if (input[j].StartsWith("R"))
                        {
                            yield return new KeyValuePair<string, string>(input[i], input[j]);
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 0

Wyatt Earp
Wyatt Earp

Reputation: 1823

As Pranav and huseyin mentioned, you just need to split into two lists and then use a nested foreach to loop through them.

var startingList = new List<string> { "S1", "R1", "R2", "S2", "S3" };
List<Tuple<string, string>> result = new List<Tuple<string, string>>();

foreach(var s in startingList.Where(x => x.StartsWith("S")).ToList())
{
    foreach(var r in startingList.Where(x => x.StartsWith("R")).ToList())
    {
        result.Add(new Tuple<string, string>(s, r));
    }
}

Upvotes: 1

Related Questions