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

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -