c# - Adding a default string if list empty -
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(string.empty, (a, b) => + b)
now, i'd populate table element "---" in case list filtered down empty 1 condition can't decide on method.
what smooth way approach it?
i've tried atrocity below looks, well..., atrociously , doesn't render right, neither. see actual source code line (preceded false or true) instead of values.
item.stuff.where(e => condition(e)).count() < 1 ? "---" : item.stuff.where(e => condition(e)) .select(f => f.status) .aggregate(string.empty, (a, b) => + b)
you this. if status list reasonably small (otherwise 1 should use stringbuilder anyway , not string concatenation).
item.stuff.where(e => condition(e)) .select(f => f.status) .aggregate("---", (a, b) => (a == "---") ? b : (a + b));
it checks if default text replaced and, if was, concatenates next status element existing text mass.
this return "---" if , if it's never evaluated, i.e. if list empty. otherwise 1 same result previously.
if status
enum, , need distinct statuses, can use behavior of [flags] attribute.
if define enum this
[flags] enum status { none = 0, active = 1, inactive = 2, pending = 4, deleted = 8 ... }
you can do:
item.stuff.where(e => condition(e)) .aggregate(status.none, (a, b) => | b)
the result collection of statuses present in list, , output nicely formatted list (active, inactive, pending
) or none
if it's never run.
Comments
Post a Comment