Results 1 to 8 of 8

Thread: Flatten list problem - cannot be inferred from usage

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Flatten list problem - cannot be inferred from usage

    So I have a List<> of items.
    each item has a list of other items (grandparent > parent > child > children etc...)

    I want to now flatten the list so all the items are in just one list.

    how can I do this?

    I tried:

    Code:
    List<Items> flat = structuredItems.SelectMany(m => m);
    but I get a compiler error:

    Code:
    Error	202	The type arguments for method 'System.Linq.Enumerable.SelectMany<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Flatten list problem - cannot be inferred from usage

    Your lambda doesn't make sense. The point of SelectMany is to start with one list, get a list from each item in that list and then join them all together. Your lambda doesn't get a list from the item - it just gets the item itself - so what's the point? If it worked then you'd just end up with the same list as you started with.

    Does your data form a simple hierarchical tree or are there potentially some circular references in there? If it's the former then you can use SelectMany correctly inside a Do Until loop but if it's the latter then it's going to be more complex.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: Flatten list problem - cannot be inferred from usage

    Thanks. must be the lack of sleep i have had showing my errors.

    my data from a simple hierachical tree does not have any circular references.
    So how can I flatten the list?

    so my MenuItem object contains some strings and also a List<MenuItem> of children.
    Last edited by Techno; Jan 30th, 2013 at 07:38 AM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Flatten list problem - cannot be inferred from usage

    I haven't tested this but it seems to me that you should be able to flatten a hierarchical list by calling SelectMany in a loop like so:
    csharp Code:
    1. private ToolStripMenuItem[] GetAllMenuItems(IEnumerable<ToolStripMenuItem> menuItems)
    2. {
    3.     // Start with the top-level menu items.
    4.     var allMenuItems = menuItems.ToList();
    5.  
    6.     // Get the first level of children.
    7.     var childMenuItems = allMenuItems.SelectMany(item => item.DropDownItems.Cast<ToolStripMenuItem>()).ToArray();
    8.  
    9.     // Keep going as long as there are children to add.
    10.     while (childMenuItems.Length > 0)
    11.     {
    12.         // Add the children to the list.
    13.         allMenuItems.AddRange(childMenuItems);
    14.  
    15.         // Get the next level of children.
    16.         childMenuItems = childMenuItems.SelectMany(item => item.DropDownItems.Cast<ToolStripMenuItem>()).ToArray();
    17.     }
    18.  
    19.     return allMenuItems.ToArray();
    20. }
    I have used ToolStripMenuItem there but it will work for any type that has a property whose type is a collection of itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Flatten list problem - cannot be inferred from usage

    You could loop through the parent collection that contains each sub collection:
    C# Code:
    1. List<List<int>> parentList = new List<List<int>>(new List<int>[]
    2. {
    3.     new List<int>(new int[] { 1, 2, 3 }),
    4.     new List<int>(new int[] { 1, 2, 3 }),
    5.     new List<int>(new int[] { 1, 2, 3 })
    6. });
    7.  
    8. List<int> joinedList = new List<int>();
    9. parentList.ForEach(l => joinedList.AddRange(l));
    10. Console.WriteLine(string.Join(", ", joinedList));

    And add the ranges to the final container.. Without a glance on how the structure of this "tree" is for each group/collection of items, anybody that gives you your solution could've just arrived there by luck.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Flatten list problem - cannot be inferred from usage

    Quote Originally Posted by AceInfinity View Post
    You could loop through the parent collection that contains each sub collection:
    C# Code:
    1. List<List<int>> parentList = new List<List<int>>(new List<int>[]
    2. {
    3.     new List<int>(new int[] { 1, 2, 3 }),
    4.     new List<int>(new int[] { 1, 2, 3 }),
    5.     new List<int>(new int[] { 1, 2, 3 })
    6. });
    7.  
    8. List<int> joinedList = new List<int>();
    9. parentList.ForEach(l => joinedList.AddRange(l));
    10. Console.WriteLine(string.Join(", ", joinedList));

    And add the ranges to the final container.
    Unless I'm missing something, that is only going to work for one level. You need to add some iteration or recursion if you want to support an arbitrary number of levels.
    Quote Originally Posted by AceInfinity View Post
    Without a glance on how the structure of this "tree" is for each group/collection of items, anybody that gives you your solution could've just arrived there by luck.
    Not really. A tree is a tree. If you're talking about a class that has a collection property of its own type then the pattern will be the same regardless of the actual definition of that class. The only thing that would change would be the type and member names.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Flatten list problem - cannot be inferred from usage

    Quote Originally Posted by jmcilhinney View Post
    Unless I'm missing something, that is only going to work for one level. You need to add some iteration or recursion if you want to support an arbitrary number of levels.
    Yes: "Without a glance on how the structure of this "tree" is for each group/collection of items, anybody that gives you your solution could've just arrived there by luck."

    You quoted and replied to this, so it didn't go without notice, but what you missed was my meaning behind why I posted that, before you took it into your own definition for what I meant.

    Quote Originally Posted by jmcilhinney View Post
    Not really. A tree is a tree. If you're talking about a class that has a collection property of its own type then the pattern will be the same regardless of the actual definition of that class. The only thing that would change would be the type and member names.
    You just said for yourself (in the same post) that it would only work for one level, so a tree is a tree, but as I said, It depends on the structure of the "tree". It can be many levels, or just one level... Please don't reply debating semantics that a tree is a tree regardless of it's levels, and the structure is still the same structure, just with different levels, because this is what I originally meant when I posted, when I referenced the "structure"; the levels or depth to the overall branches of the "tree".

    For all we know without actually seeing the tree, it could just be a leftover stump, cut down many years ago with no branches. If I was to take this analogy further, it could be a spruce tree, an apple tree, etc... There's no telling the structure of the tree, just because "it's a tree", and the same thing applies here.

    If it was really complex with many branches and different numbers of branches in some areas, then he could introduce a recursive function to retrieve the items. But I still haven't gotten a clear picture of what data he's dealing with.

    ~Ace
    Last edited by AceInfinity; Feb 3rd, 2013 at 11:08 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Flatten list problem - cannot be inferred from usage

    Quote Originally Posted by AceInfinity View Post
    For all we know without actually seeing the tree, it could just be a leftover stump, cut down many years ago with no branches. If I was to take this analogy further, it could be a spruce tree, an apple tree, etc... There's no telling the structure of the tree, just because "it's a tree", and the same thing applies here.

    If it was really complex with many branches and different numbers of branches in some areas, then he could introduce a recursive function to retrieve the items. But I still haven't gotten a clear picture of what data he's dealing with.

    ~Ace
    Well, we know this much, because we were told at the outset:
    Quote Originally Posted by Techno
    each item has a list of other items (grandparent > parent > child > children etc...)
    The code I provided will work regardless of the number of levels and there was no luck involved in that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width