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 6934635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:59:09+00:00 2026-05-27T11:59:09+00:00

I have a List<List<List<Foo>>> and I would like to flatten this to List<new {Foo,

  • 0

I have a List<List<List<Foo>>> and I would like to flatten this to List<new {Foo, Ndx}> where Ndx is the index from the outermost List. For example, if I had something like:

new List(){
    new List(){
        new List(){ new Foo("a"), new Foo("b")},
        new List(){ new Foo("c")}},
    new List(){
        new List(){ new Foo("x"), new Foo("y")}}}

I might end up with Ndx of 0 for “a”, “b”, & “c” and 1 for “x” & “y”. Someone have a LINQ solution?

  • 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-27T11:59:10+00:00Added an answer on May 27, 2026 at 11:59 am

    Bit fiddly, but you can do it like this:

    IEnumerable<Tuple<Foo,int>> result =
        tree.SelectMany(
            (L1,i) => L1.SelectMany(
                L2 => L2.Select(
                    k => Tuple.Create(k,i)
                )
            )
        );
    

    A compilable version is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    class Foo
    {
        public string s;
    
        public Foo(string s)
        {
            this.s = s;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var tree = new List<List<List<Foo>>>
            {
                new List<List<Foo>>
                {
                    new List<Foo> { new Foo("a"), new Foo("b") },
                    new List<Foo> { new Foo("c") }
                },
                new List<List<Foo>>
                {
                    new List<Foo> { new Foo("x"), new Foo("y") }
                }
            };
    
            IEnumerable<Tuple<Foo,int>> result = tree.SelectMany((L1,i) => L1.SelectMany(L2 => L2.Select(k => Tuple.Create(k,i))));
            foreach(var si in result)
            {
                Console.WriteLine(si.Item1.s + ' ' + si.Item2);
            }
        }
    }
    

    EDIT: As @sll points out, this solution requires .NET 4 due to the use of Tuple. It wouldn’t be too hard to adapt though if necessary.

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

Sidebar

Related Questions

I have a list of dictionaries like in this example: listofdict = [{'name': 'Foo',
I have a list of variable names, like this: ['foo', 'bar', 'baz'] (I originally
I have a list of files that look like this: Input /foo/bar/baz/d4dc7c496100e8ce0166e84699b4e267fe652faeb070db18c76669d1c6f69f92.mp4 /foo/baz/bar/60d24a24f19a6b6c1c4734e0f288720c9ce429bc41c2620d32e01e934bfcd344.mp4 /bar/baz/foo/cd53fe086717a9f6fecb1d0567f6d76e93c48d7790c55e83e83dd1c43251e40e.mp4
I have code that looks something like this: self.ui.foo.setEnabled(False) self.ui.bar.setEnabled(False) self.ui.item.setEnabled(False) self.ui.item2.setEnabled(False) self.ui.item3.setEnabled(False) And
I have code that looks something like this: self.ui.foo = False self.ui.bar = False
I have a query that is basically like this: SELECT foo FROM bar where
I have a class Foo which overrides equals() and hashCode() properly. I would like
This is in teradata. For many good reasons, I would like to have a
I have an unordered list like so: <ul class=foo> <li id=asdf> <span class=indexnumber></span> <span
Say I have a matrix that looks something like this: {{foobar, 77},{faabar, 81},{foobur, 22},{faabaa,

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.