c# - How to get all elements in collection when building an array of items? -
i have next code works great returns xelement object instance properties on class:
public myclass1 myclass1 {get; set;} public myclass2 myclass2 {get; set;} var elements = new[] { //calls .getxml() homecoming xelement instance this.myclass1.getxml(), this.myclass2.getxml() }; homecoming new xelement("root", elements.where(el => el != null && !el.isempty)); problem want introduce collection via ilist myclass3 , have no thought on how of items in collection elements array. want next now:
public myclass1 myclass1 {get; set;} public myclass2 myclass2 {get; set;} public ilist<myclass3> myclass3 {get; set;} var elements = new[] { this.myclass1.getxml(), this.myclass2.getxml(), this.myclass3.tolist().select(x => x.getxml()) //not working }; i have tried using combinations of select , changed ilist list see if .foreach work , didn't either. error i'm getting is: "no best type found implicitly typed array" regardless of method try.
how can of elements ilist property instance elements array? help appreciated, thanks!
edit: order must preserved (i.e. myclass1, myclass2, myclass3), if introduce element myclass4 must come after myclass3. reasoning because i'm composing xml document specific structure.
i suspect want like:
var elements = new[] { myclass1.getxml(), myclass2.getxml() } .concat(myclass3.select(x => x.getxml()) .toarray(); with myclass4 after myclass3:
var elements = new[] { myclass1.getxml(), myclass2.getxml() } .concat(myclass3.select(x => x.getxml()) .concat(new[] { myclass4.getxml() }) .toarray(); or putting getxml in 1 place:
// relies on covariance in .net 4 var elements = new mybase[] { myclass1, myclass2 } .concat(myclass3) .concat(new[] { myclass4 }) .select(x => x.getxml()) .toarray(); or without using linq @ until end:
var items = new list<baseclass>(); items.add(myclass1); items.add(myclass2); items.addrange(myclass3); items.add(myclass4); var elemets = items.select(x => x.getxml()).toarray(); c# linq
No comments:
Post a Comment