Search code examples
c#listentity-frameworklinqdata-structures

create unique binary lists from elements in a list


I want to create unique binary lists from the elements in this list.

for example;

  `["Jack", "John", "Ally"] ---> ["Jack", "John"], ["Jack", "Ally"], ["John", "Ally]`


 ["Jack", "John", "Ally", "Emmy"] --->
 ["Jack", "John"], ["Jack", "Ally"], ["Jack", "Emmy"],        
 ["John", "Ally"], ["John", "Emmy"], 
 ["Ally", "Emmy"]`

but the same values will not repeat. then i want to save these binary lists in database.

`var data = new Names() {
   Name1 = "Jack",
   Name2 = "John"
};

dbContext.Names.Add(data);`

how can I do that?


Solution

  • you can run two for loops..

    List<string> names = new List<string> { "Jack", "John", "Ally", "Emmy" };
    List<List<string>> ls = new List<List<string>>();
    
    for (int i = 0; i < names.Count; i++)
    {
        for (int j = i + 1; j < names.Count; j++)
        {
            ls.Add(new List<string> { names[i], names[j] });
        }
    }