c# - Listing a field from all the entities in a collection -
i'm writing out aggregated list of statuses. works fine except situation there none. @ moment, null rendered , position empty.
item.stuff.where(e => condition(e)) .select(f => f.status) .aggregate("---", (a, b) => (a == "---") ? b : (a + b));
i've got a suggestion solution , improvement follows.
[flags] enum status { none = 0, active = 1, inactive = 2, pending = 4, deleted = 8 } item.stuff.where(e => condition(e)) .aggregate(status.none, (a, b) => | b.status)
as i'm great fan of simpler syntax, love skipping select part. however, result not elements listed. in fact, single 1 listed (instead of 5 previously) , none appears there single status before.
perhaps i'm brain-stuck can't see why. and, consequently, not should it, neither... :(
if statuses coming extenal system (i.e. database) might need renumber statuses there well.
note works if use power-of-two values values of statuses.
eg. if use
[flags] enum status { none = 0, active = 1, inactive = 2, pending = 3, deleted = 4 }
then status.active | status.inactive
return status.pending
(1+2=3).
also, way list of distinct used statuses, i.e. if have 10 active , 5 pending items, you'll active, pending
result, without frequency information.
if need information too, suggest doing grouping, like:
string.join(", ", item.stuff.where(e => condition(e)) .select(f => f.status) .groupby(status => status) .select(group => string.format("{0} {1}", group.count(), group.key));
this result in "10 active, 5 pending" or empty string if there no groups.
edit: think of it, string.join
might better solution original question (ienumerable.aggregate
great method, might overkill). like:
string.join(", ", item.stuff.where(e => condition(e)).select(f => f.status))
Comments
Post a Comment