Filtering out values from a C# Generic Dictionary

后端 未结 6 1952
难免孤独
难免孤独 2020-12-02 16:29

I have a C# dictionary, Dictionary that I need to be filtered based on a property of MyObject.

For example, I want to

相关标签:
6条回答
  • 2020-12-02 16:50

    You can simply use the Linq where clause:

    var filtered = from kvp in myDictionary
                   where !kvp.Value.BooleanProperty
                   select kvp
    
    0 讨论(0)
  • 2020-12-02 16:50

    I added the following extension method for my project which allows you to filter an IDictionary.

    public static IDictionary<keyType, valType> KeepWhen<keyType, valType>(
        this IDictionary<keyType, valType> dict,
        Predicate<valType> predicate
    ) {
        return dict.Aggregate(
            new Dictionary<keyType, valType>(),
            (result, keyValPair) =>
            {
                var key = keyValPair.Key;
                var val = keyValPair.Value;
    
                if (predicate(val))
                    result.Add(key, val);
    
                return result;
            }
        );
    }
    

    Usage:

    IDictionary<int, Person> oldPeople = personIdToPerson.KeepWhen(p => p.age > 29);
    
    0 讨论(0)
  • 2020-12-02 16:54
        public static Dictionary<TKey, TValue> Where<TKey, TValue>(this Dictionary<TKey, TValue> instance, Func<KeyValuePair<TKey, TValue>, bool> predicate)
        {
            return Enumerable.Where(instance, predicate)
                             .ToDictionary(item => item.Key, item => item.Value);
        }
    
    0 讨论(0)
  • 2020-12-02 17:01

    Since Dictionary implements IEnumerable<KeyValuePair<Key, Value>>, you can just use Where:

    var matches = dictionary.Where(kvp => !kvp.Value.BooleanProperty);
    

    To recreate a new dictionary if you need it, use the ToDictionary method.

    0 讨论(0)
  • 2020-12-02 17:06

    If you don't care about creating a new dictionary with the desired items and throwing away the old one, simply try:

    dic = dic.Where(i => i.Value.BooleanProperty)
             .ToDictionary(i => i.Key, i => i.Value);
    

    If you can't create a new dictionary and need to alter the old one for some reason (like when it's externally referenced and you can't update all the references:

    foreach (var item in dic.Where(item => !item.Value.BooleanProperty).ToList())
        dic.Remove(item.Key);
    

    Note that ToList is necessary here since you're modifying the underlying collection. If you change the underlying collection, the enumerator working on it to query the values will be unusable and will throw an exception in the next loop iteration. ToList caches the values before altering the dictionary at all.

    0 讨论(0)
  • 2020-12-02 17:08

    Here is a general solution, working not only for boolean properties of the values.

    Method

    Reminder: Extension methods must be placed in static classes. Dont forget the using System.Linq; statement at the top of the source file.

        /// <summary>
        /// Creates a filtered copy of this dictionary, using the given predicate.
        /// </summary>
        public static Dictionary<K, V> Filter<K, V>(this Dictionary<K, V> dict,
                Predicate<KeyValuePair<K, V>> pred) {
            return dict.Where(it => pred(it)).ToDictionary(it => it.Key, it => it.Value);
        }
    

    Usage

    Example:

        var onlyWithPositiveValues = allNumbers.Filter(it => it.Value > 0);
    
    0 讨论(0)
提交回复
热议问题