|
-
Jan 30th, 2013, 06:10 AM
#1
Thread Starter
PowerPoster
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.
-
Jan 30th, 2013, 07:06 AM
#2
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.
-
Jan 30th, 2013, 07:34 AM
#3
Thread Starter
PowerPoster
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.
-
Jan 30th, 2013, 07:27 PM
#4
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:
private ToolStripMenuItem[] GetAllMenuItems(IEnumerable<ToolStripMenuItem> menuItems) { // Start with the top-level menu items. var allMenuItems = menuItems.ToList(); // Get the first level of children. var childMenuItems = allMenuItems.SelectMany(item => item.DropDownItems.Cast<ToolStripMenuItem>()).ToArray(); // Keep going as long as there are children to add. while (childMenuItems.Length > 0) { // Add the children to the list. allMenuItems.AddRange(childMenuItems); // Get the next level of children. childMenuItems = childMenuItems.SelectMany(item => item.DropDownItems.Cast<ToolStripMenuItem>()).ToArray(); } return allMenuItems.ToArray(); }
I have used ToolStripMenuItem there but it will work for any type that has a property whose type is a collection of itself.
-
Feb 3rd, 2013, 10:25 PM
#5
Re: Flatten list problem - cannot be inferred from usage
You could loop through the parent collection that contains each sub collection:
C# Code:
List<List<int>> parentList = new List<List<int>>(new List<int>[]
{
new List<int>(new int[] { 1, 2, 3 }),
new List<int>(new int[] { 1, 2, 3 }),
new List<int>(new int[] { 1, 2, 3 })
});
List<int> joinedList = new List<int>();
parentList.ForEach(l => joinedList.AddRange(l));
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Feb 3rd, 2013, 10:39 PM
#6
Re: Flatten list problem - cannot be inferred from usage
 Originally Posted by AceInfinity
You could loop through the parent collection that contains each sub collection:
C# Code:
List<List<int>> parentList = new List<List<int>>(new List<int>[] { new List<int>(new int[] { 1, 2, 3 }), new List<int>(new int[] { 1, 2, 3 }), new List<int>(new int[] { 1, 2, 3 }) }); List<int> joinedList = new List<int>(); parentList.ForEach(l => joinedList.AddRange(l)); 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.
 Originally Posted by AceInfinity
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.
-
Feb 3rd, 2013, 10:56 PM
#7
Re: Flatten list problem - cannot be inferred from usage
 Originally Posted by jmcilhinney
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.
 Originally Posted by jmcilhinney
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Feb 3rd, 2013, 11:35 PM
#8
Re: Flatten list problem - cannot be inferred from usage
 Originally Posted by AceInfinity
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:
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|