c# - Merge the values of multiple dictionaries into one list -
i want merge values of multiple dictionaries (3 exact) list. current solution uses linq first combine dictionaries , converts values list.
private list<part> allparts() { return walls.concat(floors) .concat(columns) .todictionary(kvp => kvp.key, kvp => kvp.value) .values .tolist(); }
merging lists first seems redundant. how can improve this?
you can simplify code concatenating dictionaries , selecting values without converting dictionary:
return walls.concat(floors) .concat(columns) .select(kvp => kvp.value) .tolist();
it looks shortest , readable solution. can avoid concatenating collections taking values only:
return walls.values .concat(floors.values) .concat(columns.values) .tolist();
however, not see readability, maintainability or performance improvements here.
p.s. assumed there no duplicates in dictionaries. code contain duplicated values while todictionary
approach throw exceptions on key duplication.
Comments
Post a Comment