linq - SelectMany while Referencing Parent -
given
list<foo> foos; public class foo { public string name { get; set; } public list<bar> bars { get; set; } } public class bar { public double score { get; set; } }
i'm trying output, each object in foos
, property foo.name along maximum bar.score. see how maximum score:
foos.selectmany(f => f.bars).maxby(b => b.score).select(b => b.score);
is there way corresponding name well, without adding reference bar foo? if made following change
public class bar { public foo foo { get; set; } // can't add public double score { get; set; } }
i do
foos.selectmany(f => f.bars).maxby(b => b.score) .select(b => new { name = b.foo.name, score = b.score) });
however in real case, cannot add reference.
you don't need use selectmany
, use select
max
:
var result = foos .select(f => new { name = f.name, maxscore = f.bars.max(b => b.score) });
Comments
Post a Comment