c# - How to order IGrouping without changing its type? -
i have oject of type igrouping , order elements inside group without changing type of object.
in other words, have
var tmp = group.orderby(x => x); with group being of type igrouping<int, someanonymousclass> , want tmp of type igrouping<int, someanonymousclass> well.
one workaround change dictionary or new anonymous class, want tmp specificly of type igrouping<int, someanonymousclass>.
in other words: want sort elemnts of igrouping<int, someanonymousclass> object. if use orderby changes type of group iorderedenumerable , cannot access group.key anymore. how can sustain type of group?
example:
var states = simulationpanel.innerpanel.children.oftype<statebar>().where(x => x.issensorstate()) .groupby(x => (sensorelement)x.bauplanelement, x => new { start = (decimal)(x.margin.left / simulationpanel.zoom), width = (decimal)(x.width / simulationpanel.zoom), state = x.state }); var group = states.first(); var tmp = group.orderby(x => x.start); var key = tmp.key; //this not work, because tmp not of type igrouping i know use orderby before grouping. don't want that, though.
if can, put ordering earlier, e.g.
var states = simulationpanel.innerpanel .children .oftype<statebar>() .where(x => x.issensorstate()) .orderby(x => (decimal)(x.margin.left / simulationpanel.zoom)) .groupby(...); (or put select before orderby , make groupby simpler.)
if need though, write own igrouping<tkey, telement> implementation , extension method order elements , retain them in list:
public static class groupextensions { public igrouping<tkey, telement, torderkey> orderby (this igrouping<tkey, telement> grouping, func<telement, torderkey> orderkeyselector) { return new groupingimpl<tkey, telement> (grouping.key, grouping.orderby(orderkeyselector)); } private class groupingimpl<tkey, telement> : igrouping<tkey, telement> { private readonly tkey key; private readonly list<telement> elements; internal groupingimpl(tkey key, ienumerable<telement> elements) { this.key = key; this.elements = elements.tolist(); } public tkey key { { return key; } } public ienumerator<telement> getenumerator() { return elements.getenumerator(); } ienumerator ienumerable.getenumerator() { return getenumerator(); } } }
Comments
Post a Comment