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

  • SEARCH
  • Home
  • 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 8004925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:04:08+00:00 2026-06-04T17:04:08+00:00

Lets say I have an object which contains a persons name, and their city

  • 0

Lets say I have an object which contains a persons name, and their city of origin.

public class personDetails
{
    public string City;
    public string Name;
}

And I have a List with the following entries added.

Name   City
John | London
Jane | London
Tom  | New York
Bob  | New York
Fred | New York

What I’m looking for is all possible combinations of names, grouped by city.

John Tom
John Bob  
John Fred
Jane Tom
Jane Bob
Jane Fred

I can do this if I know in advance the number of groups, by using the following code

List<personDetails> personList = new List<personDetails>();
//populate list

var groupedPersons = personList.GroupBy(c => c.City);
foreach (var item1 in groupedPersons[0])
{
    foreach (var item2 in groupedPersons[1])
    {
        Console.WriteLine(item1.Name + " " + item2.Name);
    }          
}

However, this only works if i know the number of groups in advance, and quickly becomes unwieldy as the amount of groups grow larger. I’m sure that there is an elegant way to do this using LINQ, can anybody shed some light?

  • 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-06-04T17:04:10+00:00Added an answer on June 4, 2026 at 5:04 pm

    We’ll start with the following snippet of code taken verbatum from here. (It’s a good link, worth reading).

    public static class MyExtensions
    {
        public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
        {
            IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
            return sequences.Aggregate(
                emptyProduct,
                (accumulator, sequence) =>
                from accseq in accumulator
                from item in sequence
                select accseq.Concat(new[] { item }));
        }
    }
    

    After that all we need to do is:

    var groupedPersons = personList.GroupBy(c => c.City)
        //need an enumerable of enumerables, not an enumerable of groupings, 
        //because the method isn't covariant.
        .Select(group => group.AsEnumerable());
    
    var results = groupedPersons.CartesianProduct();
    foreach (var group in results)
    {
        foreach (var person in group)
        {
            Console.Write(person.Name + " ");
        }
        System.Console.WriteLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a List<object> which is passed into a class as an
I have the following City class. Each city object contains a dictionary which keys
Lets say that I have one component which is doing something with Workbook object
Lets say I have an object with two boolean properties: public bool AdvancedMode{ get;
Suppose we have an object which represent a box. class Box { public: int
Lets say I have a structure like this: Object A contains an instance of
My question is quite simple I have a class which contains a (private) object
So lets say I have a class called Post that contains an IList I
Let's say you have a class called Customer, which contains the following fields: UserName
Say I have an object called data which contains a variety of information. Let's

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.