LINQ将字典转换为查找

时间:2009-09-23 06:07:39

标签: linq dictionary lookup

我有一个Dictionary<MyType, List<MyOtherType>>类型的变量 我想将其转换为Lookup<MyType, MyOtehrType>

我想首先使用Lambda函数,展平字典,然后使用ToLookup()将其转换为Lookup。我被困在字典里。我想过使用SelectMany却无法使用它。任何人都知道该怎么做?

5 个答案:

答案 0 :(得分:19)

与Jon的方法相同,但避免创建匿名类型:

var lookup = dictionary
            .SelectMany(p => p.Value, Tuple.Create)
            .ToLookup(p => p.Item1.Key, p => p.Item2);

答案 1 :(得分:16)

怎么样:

var lookup = dictionary.SelectMany(pair => pair.Value,
                                   (pair, Value) => new { pair.Key, Value })
                       .ToLookup(pair => pair.Key, pair => pair.Value);

当字典已经将所有信息分组得恰当时,确实感觉有点浪费,但我看不到一个简单的方法。当然,您可以使用字典中的包装器自己实现ILookup<TKey, TValue> ...

答案 2 :(得分:0)

这里已经有一些答案了,但是请把它放在这里作为参考。 这会将具有值列表的字典翻转为将这些值作为查找列表的键。

var myLookup = myDict.SelectMany(p => p.Value, 
        (pair, id) => Tuple.Create(id, pair.Key))
    .ToLookup(p => p.Item1, p => p.Item2);

带注释的


var myLookup = myDict.SelectMany(
        // specify that the select many is to be based off the Value which is a list of items
        p => p.Value, 
        // Using the individual items from that list, create a tuple of that item and the dictionary key it was under
        (pair, id) => Tuple.Create(id, pair.Key))
        // use the item as the lookup key, and put the original dictionary key (that
        // had that value underneath them) in the list of lookup values.
    .ToLookup(p => p.Item1, p => p.Item2);

答案 3 :(得分:0)

不是问题的答案,但我认为这是相关信息,应在此处发布。

您应该考虑一些极端情况。所有这些都与字典项有关,这些项具有键,但是没有价值。

这是预期的行为。为不同目的设计的字典和查找。

var dic = new Dictionary<bool, IEnumerable<bool?>> { [true] = null };
var lookup = dic.ToLookup();

Assert.AreEqual(1, dic.Count);
Assert.AreEqual(0, lookup.Count);

Assert.IsTrue(dic.ContainsKey(true));
Assert.IsFalse(lookup.Contains(true));

Assert.IsFalse(dic.ContainsKey(false));
Assert.IsFalse(lookup.Contains(false));

dic[false] -> Exception
lookup[false] -> bool?[0]

答案 4 :(得分:0)

聚会迟到了,但我认为这应该可行,无需再次枚举所有内容并创建临时元组/匿名类型。

public static ILookup<TKey, TElement> ToLookup<TKey, TElement>(
    this IEnumerable<TKey> keys,
    Func<TKey, IEnumerable<TElement>> selector)
{
    return new ManualLookup<TKey, TElement>(keys, selector);
}

private class ManualLookup<TKey, TElement> : ILookup<TKey, TElement>
{
    private IEnumerable<TKey> _keys;
    private Func<TKey, IEnumerable<TElement>> _selector;

    public ManualLookup(IEnumerable<TKey> keys, Func<TKey, IEnumerable<TElement>> selector)
    {
        _keys = keys;
        _selector = selector;
    }

    public IEnumerable<TElement> this[TKey key] => _selector(key);

    public int Count => _keys.Count();

    public bool Contains(TKey key) => _keys.Contains(key);

    public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator() => _keys
        .Select(key => new ManualGrouping<TKey, TElement>(key, _selector(key)))
        .GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

private class ManualGrouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    private TKey _key;
    private IEnumerable<TElement> _enumerable;

    public ManualGrouping(TKey key, IEnumerable<TElement> enumerable)
    {
        _key = key;
        _enumerable = enumerable;
    }

    public TKey Key => _key;

    public IEnumerator<TElement> GetEnumerator() => _enumerable.GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

然后您可以执行以下操作:

Dictionary<MyType, List<MyOtherType>> dictionary;
return dictionary.Keys.ToLookup(key => 
{
    if (dictionary.TryGetValue(key, out var list)
    {
        return list;
    }

    return Enumerable.Empty<MyOtherType>();
});