首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ILookup 4.0中修改C#对象?

如何在ILookup 4.0中修改C#对象?
EN

Stack Overflow用户
提问于 2010-01-02 07:19:35
回答 3查看 2.6K关注 0票数 6

在发布.NET 3.5之前,我使用

代码语言:javascript
复制
Dictionary<TKey, List<TValue>> 

用于包含数据。但我刚刚发现,.NET 3.5提供了新的集合类型,即ILookup类,可以表示旧的复杂数据类型。

我总是使用( ILookup方法)来创建ToLookup对象。但是我不知道如何修改ILookup对象。

有可能吗?或者我需要使用union方法创建,然后再次调用ToLookup方法。

谢谢,

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-01-02 07:22:34

你不知道,这是不可改变的。您列出了两个合理的选项;要么使用子集合的字典,要么继续创建新的查找。

票数 4
EN

Stack Overflow用户

发布于 2010-12-07 13:47:33

下面是一个可以操作的ILookup实现的示例。它围绕着元素的Dictionary of List。它是完全通用的。我想不出更好的名字了。:)

代码语言:javascript
复制
public class LookupDictionary<TKey, TElement> : ILookup<TKey, TElement>
{
    private Dictionary<TKey, List<TElement>> _dicLookup = new Dictionary<TKey, List<TElement>>();

    public LookupDictionary()
    {

    }

    public LookupDictionary(ILookup<TKey, TElement> a_lookup)
    {
        foreach (var grouping in a_lookup)
        {
            foreach (var element in grouping)
                AddElement(grouping.Key, element);
        }
    }

    public IEnumerable<TElement> AllElements
    {
        get
        {
            return (from key in _dicLookup.Keys
                    select _dicLookup[key])
                    .SelectMany(list => list);
        }
    }

    public int Count
    {
        get 
        {
            return AllElements.Count();
        }
    }

    public IEnumerable<TElement> this[TKey a_key]
    {
        get 
        {
            List<TElement> list;
            if (_dicLookup.TryGetValue(a_key, out list))
                return list;

            return new TElement[0];
        }
    }

    public bool Contains(TKey a_key)
    {
        return _dicLookup.ContainsKey(a_key);
    }

    public void Add(TKey a_key, TElement a_element)
    {
        AddElement(a_key, a_element);
    }

    public void RemoveKey(TKey a_key)
    {
        _dicLookup.Remove(a_key);
    }

    public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator()
    {
        return GetGroupings().GetEnumerator();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return (GetGroupings() as System.Collections.IEnumerable).GetEnumerator();
    }

    private void AddElement(TKey a_key, TElement a_element)
    {
        List<TElement> list;

        if (!_dicLookup.TryGetValue(a_key, out list))
        {
            list = new List<TElement>();
            _dicLookup.Add(a_key, list);
        }

        list.Add(a_element);
    }

    private IEnumerable<IGrouping<TKey, TElement>> GetGroupings()
    {
        return from key in _dicLookup.Keys
               select new LookupDictionaryGrouping<TKey, TElement>
               {
                   Key = key,
                   Elements = _dicLookup[key]
               } as IGrouping<TKey, TElement>;
    }
}

public class LookupDictionaryGrouping<TKey, TElement> : IGrouping<TKey, TElement>
{
    public TKey Key
    {
        get;
        set;
    }

    public IEnumerable<TElement> Elements
    {
        get;
        set;
    }

    public IEnumerator<TElement> GetEnumerator()
    {
        return Elements.GetEnumerator();
    }


    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return (Elements as System.Collections.IEnumerable).GetEnumerator();
    }
}
票数 4
EN

Stack Overflow用户

发布于 2017-05-17 15:01:21

正如mquander提到的,查找是不变的。但是,您可以使用附加或删除的值构建新的查找。

代码语言:javascript
复制
// Add a new value
myLookup = myLookup
    .SelectMany(l => l.Select(v => new {l.Key, Value = v}))
    .Union(new[] {new {Key = myNewKey, Value = myNewValue}})
    .ToLookup(a => a.Key, a => a.Value);

// Remove an old value
myLookup = myLookup
    .SelectMany(l => l.Select(v => new {l.Key, Value = v}))
    .Where(a => a.Value != myOldValue)
    .ToLookup(a => a.Key, a => a.Value);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1990868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档