c# - LINQ: How to Append List of Elements Into Another List -


i have following class

public class element {   public list<int> ints   {      get;private set;   } } 

given list<element>, how find list of ints inside list<element> using linq?

i can use following code

public static list<int> findints(list<element> elements) {  var ints = new list<int>();  foreach(var element in elements)  {   ints.addrange(element.ints);  }  return ints;  } } 

but ugly , long winded want vomit every time write it.

any ideas?

return (from el in elements         in el.ints         select i).tolist(); 

or maybe just:

return new list<int>(elements.selectmany(el => el.ints)); 

btw, you'll want initialise list:

public element() {     ints = new list<int>(); } 

Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c# - Making TableLayoutPanel Act Like An HTML Table (Cells That Resize Automatically Around Text) -