Create ILookup <int, int> from IDictionary <int, IEnumerable <int>>
Is there an elegant way to convert
IDictionary<int, IEnumerable<int>>in ILookup<int,int>? As far as I know, it should be identical, but I believe that the search is more clear.
The story behind this is more complicated, but I have to choose List of id along with its lists of related identifiers:
masters
.Select(m => new {masterId = m.Id, childIds = m.Children.Select(c => c.Id)})
.ToDictionary(k => masterId, v => v.childIds)
I would be happy to directly select Search, but I do not know if this is possible.
An example of the type of the main variable can be simple:
public class Master
{
public int Id { get; set; }
public List<Master> Children { get; set; }
}
As Lasse W. Carlsen suggested in the comments, you can create a shell type that provides ILookup:
public class LookupDictionary<TKey, TElement> : ILookup<TKey, TElement>
{
private readonly IDictionary<TKey, IEnumerable<TElement>> _dic;
public LookupDictionary(IDictionary<TKey, IEnumerable<TElement>> dic)
{
_dic = dic;
}
public int Count
{
get { return _dic.Values.Sum(x => x.Count()); }
}
public IEnumerable<TElement> this[TKey key]
{
get { return _dic.ContainsKey(key) ? _dic[key] : Enumerable.Empty<TElement>(); }
}
public bool Contains(TKey key)
{
return _dic.ContainsKey(key);
}
public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
{
return _dic.Select(kv => new LookupDictionaryGrouping(kv)).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
class LookupDictionaryGrouping : IGrouping<TKey, TElement>
{
private KeyValuePair<TKey, IEnumerable<TElement>> _kvp;
public TKey Key
{
get { return _kvp.Key; }
}
public IEnumerator<TElement> GetEnumerator()
{
return _kvp.Value.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public LookupDictionaryGrouping(KeyValuePair<TKey, IEnumerable<TElement>> kvp)
{
_kvp = kvp;
}
}
}
You can do it - a little more readable than pure lambda ... :)
Dictionary<int, IEnumerable<int>> dict = new Dictionary<int, IEnumerable<int>>();
dict.Add(1, new int[] {1, 2, 3});
dict.Add(2, new int[] {4, 5, 6});
dict.Add(3, new int[] {4, 5, 6});
var lookup = (from kv in dict
from v in kv.Value
select new KeyValuePair<int, int>(kv.Key, v)).ToLookup(k=>k.Key, v=>v.Value);
If I understand correctly, you want to flatten your collection if you can do it this way:
masters.SelectMany(x => x.Children, (x, y) => new { ParentId = x.Id, ChildId = y.Id })
.ToLookup(x => x.ParentId, y => y.ChildId);
So you get ILookup<int,int>. In addition, you do not need a conversion Dictionary. But that is pretty much a conservation since Dictionary.