linq - How to pass to Razor view an anonymous type that contains collections? -


i attempting pass collection of anonymous types has in collection of types razor view engine.

i using following extension pass dynamic objects view.

 public static expandoobject toexpando(this object anonymousobject) { idictionary<string, object> anonymousdictionary = new routevaluedictionary(anonymousobject); idictionary<string, object> expando = new expandoobject(); foreach (var item in anonymousdictionary) expando.add(item); return (expandoobject)expando; } 

it works simple anonymous objects such as:

mylinqstatement.select(x => new { foo = x.f, bar = x.b }.toexpando()); 

however, have been unsuccessful in passing more complex type such as:

mylinqstatement.groupby(y => y.foo).select(x => new { foo = x.key, bar = x.select(y => y.b) }.toexpando()); 

i have made several attempts rewrite ladder statement (i showed simplest), property want (the value of y.b) never accessible within view.

any appreciated.

i answered own question.

mylinqstatement.groupby(y => y.foo).select(x => new { foo = x.key, bar = x.select(y => new { someproperty = y.b) }.toexpando())}.toexpando()); 

in view...

@foreach (var item in model) { @item.foo foreach (var baritem in item.bar) //<--- notice it's not 'in item'. { @baritem .someproperty } } 

hopefully can avoid unnecessary headache.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -