C# flatten a list of lists
WebFeb 25, 2013 · public static List Flatten (Category root) { var flattened = new List {root}; var children = root.Children; if (children != null) { foreach (var child in children) { flattened.AddRange (Flatten (child)); } } return flattened; } Share Improve this answer Follow answered Oct 11, 2010 at 15:35 E.Z. Hart 5,707 1 31 24 WebSep 14, 2016 · Add a comment -1 That should give you what you need: var flattened = stockItems .Select (x => new { StockName = x.StockName, WarehouseNames = x.Warehouses .Select (y => y.WarehouseName) .ToList () }) .ToList (); It will result in a collection of items that contain StockName and a list of WarehouseName strings.
C# flatten a list of lists
Did you know?
WebApr 5, 2024 · Flatten a list - Rosetta Code Task Write a function to flatten the nesting in an arbitrary list of values. Your program should work on the equivalent of this list: [[1], 2, … WebFeb 28, 2024 · Using Linq what will be the best way of making a flattened list with these 2 objects lists. public class DataDto { public string StudentID { get; set; } public List Scores { get; set; } } public class ScoreDto { public string ExamID { get; set; } public double Mark { get; set; } }
Addresses{ get; set;} } // … WebDec 3, 2016 · To make this more readable, you can make a simple function: def flatten_list (deep_list: list [list [object]]): return list (chain.from_iterable (deep_list)). The type hinting improves the clarity of what's going on (modern IDEs would interpret this as returning a list [object] type). – Chris Collett Oct 25, 2024 at 14:34 2
WebNov 20, 2009 · I have Items from a certain source (populated from somewhere else): public class ItemsFromSource{ public ItemsFromSource(string name){ this.SourceName = name; Items = new List< WebFeb 10, 2024 · I would like to extract the strings from the list above, not allowing the lists containing the strings to have more than one item. That is, I need to have the following result: { "Apple", "Banana" } So far I have been trying the following: var result = fruits .Where (x => x != null) .Select (x => x.SingleOrDefault ()) .Where (x => x != null);
WebMar 23, 2016 · var result = first .Zip (second, (f, s) => new Type [] {a, b}) .SelectMany (x => x); As you can see, we first use Zip to take one element from each of the lists, and combine them into a small array of two …
WebOct 8, 2013 · 11 Answers Sorted by: 32 You can do SelectMany List result = myLocationList.SelectMany (x => x.Children).ToList (); You can use where condition for some selective results like List result = myLocationList.Where (y => y.ParentID == someValue) .SelectMany (x => x.Children).ToList (); manly rotary fun runWebHere the SelectMany call takes a sequence of inputs (the lists which make the values of the dictionary) and projects each single input into another sequence of outputs - in this case … manly royal yacht clubWebFeb 23, 2012 · If you have an enumerable (or queryable) object that when enumerated returns yet more enumerable objects, such as a list of lists or an array of hash-sets, etc, it selects the elements of those objects. So in this where values is an ICollection> it returns an IEnumerable. – Jon Hanna Feb 23, 2012 … manly rugby leagueWebMar 14, 2024 · A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. manly rlfcWebApr 12, 2024 · The solution only works on one level, which is what I needed. It could be made recursive to go deeper though: var result = parents.SelectMany (person => person.Children .Prepend (person)) .Select (p => p.Name); Which then allowed me to use all the objects in the flattened lists, including the parent objects. manly rugby clubWebApr 10, 2024 · This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. manly rugby league 2022WebJul 16, 2009 · List listA = new List { 1, 2, 3, 4, 5, 6 }; List listB = new List { 11, 12, 13, 14, 15, 16 }; List> listOfLists = new List> { … manly road veterinary clinic