@john zyd, Welcome to Microsoft Q&A,
First, we need to use Groupby to group by the last 2 items.
var result = forex_pairs.GroupBy(x => new { x.Item2, x.Item3 })
Second, please use tolookup method to select the correspond item1.
Select(m => m.ToLookup(p => p.Item1.ToString())
Third, we need to select the final key.
Select(m => m.Select(n => n.Key)).ToList()
Finally, we could use the following code to get Permutations.
static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int count)
{
int i = 0;
foreach (var item in items)
{
if (count == 1)
yield return new T[] { item };
else
{
foreach (var result in GetPermutations(items.Skip(i + 1), count - 1))
yield return new T[] { item }.Concat(result);
}
++i;
}
}
Here is the completed code example:
static void Main(string[] args)
{
var A1 = ("A", "EUR/USD", "EURO");
var A2 = ("A", "CHF/USD", "CHF");
var A3 = ("A", "EUR/GBP", "EURO");
var A4 = ("B", "EUR/USD", "EURO");
var A5 = ("B", "CHF/USD", "CHF");
var A6 = ("B", "EUR/GBP", "EURO");
var A7 = ("C", "EUR/USD", "EURO");
var A8 = ("C", "CHF/USD", "CHF");
var forex_pairs = new List<(string, string, string)>(){
A1, A2, A3, A4, A5, A6, A7, A8};
var result = forex_pairs.GroupBy(x => new { x.Item2, x.Item3 }).Select(m => m.ToLookup(p => p.Item1.ToString())).Select(m => m.Select(n => n.Key)).ToList(); ;
foreach (var item in result)
{
var result1 = GetPermutations(item, 2);
foreach (var liststr in result1)
{
foreach (var str in liststr)
{
Console.WriteLine(str);
}
Console.WriteLine("-----");
}
Console.WriteLine("***");
}
}
static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int count)
{
int i = 0;
foreach (var item in items)
{
if (count == 1)
yield return new T[] { item };
else
{
foreach (var result in GetPermutations(items.Skip(i + 1), count - 1))
yield return new T[] { item }.Concat(result);
}
++i;
}
}
Result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.