How to use LINQ to select some fields with total combination from a set items.

john zyd 406 Reputation points
2023-01-18T23:22:38.06+00:00

Hello:
I have this requirements, the following is the data:
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");
List<(string, string, string)> forex_pairs = new()
{
A1, A2, A3, A4, A5, A6, A7, A8
};
I want to select the first item from the list of tuples, where the last 2 items are in the same group, also I want to list all the combinations of the first item.
In the above example, I want to get the following results:
Group#1: ("EUR/USD", "EURO") have the following List<(string, string)> members: List<("A", "B"), ("A", "C"),("A", "C"));
Group#2: ("CHF/USD", "CHF") have the following List<("A", "B"), ("A", "C"),("A", "C"));
Group#3: ("EUR/GBP", "EURO") have the following List<("A", "B"));

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,002 questions
{count} votes

Accepted answer
  1. Jack J Jun 19,376 Reputation points Microsoft Employee
    2023-01-19T02:15:28.59+00:00

    @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:

    User's image

    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.

    No comments

0 additional answers

Sort by: Most helpful