LINQ 将字典转换为查找

发布于 2024-08-05 17:35:09 字数 265 浏览 5 评论 0 原文

我有一个类型为 Dictionary>
的变量 我想将其转换为 Lookup

我想首先使用 Lambda 函数展平字典,然后使用 ToLookup() 将其转换为 Lookup。我被字典困住了。我想过使用 SelectMany 但无法让它工作。有人知道如何做吗?

I have a variable of type Dictionary<MyType, List<MyOtherType>>
I want to convert it to a Lookup<MyType, MyOtherType>.

I wanted to use Lambda functions to first, flatten the dictionary and then convert this to Lookup using the ToLookup(). I got stuck with the dictionary. I thought about using SelectMany but can't get it working. Anyone has got an idea how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

じ违心 2024-08-12 17:35:09

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

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

Same as Jon's method, but avoiding the creation of an anonymous type:

var lookup = dictionary
            .SelectMany(p => p.Value, Tuple.Create)
            .ToLookup(p => p.Item1.Key, p => p.Item2);
深海不蓝 2024-08-12 17:35:09

怎么样:

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

当字典已经将所有信息适当分组时,这样做确实感觉有点浪费,但我看不出有什么简单的方法可以解决这个问题。当然,您可以使用字典的包装器自己实现 ILookup...

How about:

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

It does feel like a little bit of a waste doing this when the dictionary already has all the information grouped appropriately, but I can't see a simple way round that. Of course you could implement ILookup<TKey, TValue> yourself with a wrapper around the dictionary...

烂柯人 2024-08-12 17:35:09

这里已经有一些答案,但将其放在这里供参考。
这会翻转带有值列表的字典,将这些值作为查找列表的键。

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

Already a few answers here, but putting this here for reference.
This flips a dictionary with a list of values, to having those values as the keys of look up list.

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

Annotated


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);
别闹i 2024-08-12 17:35:09

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

您应该考虑一些边缘情况。所有这些都与字典中的项目有关,这些项目有键,但没有值。

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

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]

Not an answer for the question, but I think this is related information and should be posted here.

There is some edge cases you should take into account. All of them about items of dictionary, which have key, but don't have value.

This is expected behavior. Dictionary and Lookup designed for different purposes.

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]
灯角 2024-08-12 17:35:09

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

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

Late to the party but I think this should work, without needing to enumerate everything again and create temporary tuples/anonymous types.

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

Then you can do something like:

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

    return Enumerable.Empty<MyOtherType>();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文