c# - Why is Dictionary.ToLookup() not working? -
i have dictionary<ttype, list<tdata>>
represents kind of internal data container. tdata elements grouped ttype.
a user may query dictionary , should given ilookup<ttype, tdata>
result. simplest query return whole data container:
public ilookup<ttype, tdata> queryeverything () { return _data.tolookup(kvp => kvp.key, kvp => kvp.value); }
however, not work. why? isn't lookup nothing more dictionary of key => ienumerable<value>
?
you try this:
public ilookup<ttype, tdata> queryeverything () { return _data.selectmany(kvp => p.value.select(x => new { kvp.key, value = x })) .tolookup(kvp => kvp.key, kvp => kvp.value); }
of course, instead of anonymous type, create keyvaluepair<ttype, tdata>
or tuple<ttype, tdata>
easily.
or perhaps better solution (if can manage refactor code) change private _data
dictionary ilookup<ttype, tdata>
, there no need convert dictionary in first place.
Comments
Post a Comment