从IDictionary <int,ienumerable <int>&gt;创建ILookup <int,int>

时间:2015-09-30 13:00:31

标签: c#

是否有任何优雅的方式如何转换

IDictionary<int, IEnumerable<int>>加入ILookup<int,int>? 据我所知它应该是相同的,但我发现查找更清楚。

这背后的故事更为复杂,但我不得不选择ID列表及其相关ID列表:

masters
    .Select(m => new {masterId = m.Id, childIds = m.Children.Select(c => c.Id)})
    .ToDictionary(k => masterId, v => v.childIds)

我很乐意直接选择Lookup,但我不知道它是否可能。

主变量类型的例子可以很简单:

public class Master
{
     public int Id { get; set; }
     public List<Master> Children { get; set; }
}

4 个答案:

答案 0 :(得分:3)

正如Lasse V. Karlsen在评论中建议的那样,您可以创建一个公开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;
        }
    }
}

答案 1 :(得分:0)

你可以做到这一点 - 比纯粹的lambdas更具可读性......:)

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);

答案 2 :(得分:0)

好吧,你可以压扁字典,然后将其转换为Lookup

dict.SelectMany(kvp -> kvp.Value, (kvp, v) => new {k = kvp.Key, v})
    .ToLookup(kvp => kvp.k, kvp => kvp.v)

但它与字典几乎完全相同,所以似乎没必要。

答案 3 :(得分:0)

如果我理解正确你想要收集你的收藏品,如果是这样你就可以这样做:

masters.SelectMany(x => x.Children, (x, y) => new { ParentId = x.Id, ChildId = y.Id })
.ToLookup(x => x.ParentId, y => y.ChildId);

所以你得到了ILookup<int,int>。此外,您不需要任何Dictionary转换。但它几乎与Dictionary一起保存。

相关问题