Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:30:26+00:00 2026-05-26T23:30:26+00:00

I have a Collection ( List ) of items ( String ). Number of

  • 0

I have a Collection (List) of items (String). Number of items in this collection will always be between 0 to 9.

I need to create all combinations of pairs and triples from this collection.
Position of item in double or triplet does not matter. So {1,2} is equal to {2,1}.

How can i achieve this? Maybe there is some nice way to do this via LINQ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:30:26+00:00Added an answer on May 26, 2026 at 11:30 pm

    In the code below I generate all unique doubles and triplets using linq. I use the fact that strings have a total ordering.

    This generates all doubles:

    string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    
    var combinations = 
        from a in items
        from b in items
        where a.CompareTo(b) < 0
        orderby a, b
        select new { A = a, B = b };
    
    foreach(var pair in combinations)
        Console.WriteLine("({0}, {1})", pair.A, pair.B);
    

    This generates all triplets:

    string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    
    var combinations = 
        from a in items
        from b in items
        from c in items
        where a.CompareTo(b) < 0 && b.CompareTo(c) < 0
        orderby a, b, c
        select new { A = a, B = b, C = c };
    
    foreach(var triplet in combinations)
        Console.WriteLine("({0}, {1}, {2})", triplet.A, triplet.B, triplet.C);
    

    Update: There is a generic solution to create all unique subsets of a specific length, and still use linq. However, you need a returntype that can contain the subset. I created a simple class LinkedNode, because to me this feels most natural in combination with linq:

    void Main()
    {
        string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    
        foreach(var combination in CreateCombinations(items, 5))
            Console.WriteLine("({0})", combination.ToString());
    }
    
    private static IEnumerable<LinkedNode> CreateCombinations(string[] items, int length)
    {
        if(length == 1)
            return items.Select(item => new LinkedNode { Value = item, Next = null });
    
        return from a in items 
            from b in CreateCombinations(items, length - 1) 
            where a.CompareTo(b.Value) < 0
            orderby a, b.Value
            select new LinkedNode<T> { Value = a, Next = b };
    }
    
    public class LinkedNode
    {
        public string Value { get; set; }
        public LinkedNode Next { get; set; }
    
        public override string ToString()
        {
            return (this.Next == null) ? Value : Value + ", " + Next.ToString();
        }
    }
    

    It should be easy to implement IEnumerable<string> on the class LinkedNode, or otherwise convert the LinkedNodes to a List<string> or HashSet<string>. Note that you can remove the line orderby a, b.Value if the order is not important.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have a number of enums and need to get them as List<string> objects
HI all I have problem when trying to convert list collection string to one
I have Collection List<Car> . How to compare each item from this collection with
This is a long shot, I know... Let's say I have a collection List<MyClass>
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
I have a collection of strings. I need to find out from this collection
I have a huge number of items to be stored in a collection. I
I have a collection named as MenuItemCollection and this derived form List< MenuItem >
I have a collection of objects that look like this: List<MyObject> objects = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.