开发者

Filtering out values from a C# Generic Dictionary

开发者 https://www.devze.com 2022-12-17 06:02 出处:网络
I have a C# dictionary, Dictionary<Gui开发者_JS百科d, MyObject> that I need to be filtered based on a property of MyObject.

I have a C# dictionary, Dictionary<Gui开发者_JS百科d, MyObject> that I need to be filtered based on a property of MyObject.

For example, I want to remove all records from the dictionary where MyObject.BooleanProperty = false. What is the best way of acheiving this?


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.


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.


You can simply use the Linq where clause:

var filtered = from kvp in myDictionary
               where !kvp.Value.BooleanProperty
               select kvp


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


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


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


Hope it fit your needs.

                Dictionary<string, string> d = new Dictionary<string, string>();
                d.Add("Product", "SELECT * FROM PRODUCT");
                d.Add("Order", "SELECT * FROM ORDER");
                d.Add("Customer", "SELECT * FROM CUSTOMER");

                var x = d.Where(kvp => kvp.Key == "Order")
                            .ToDictionary(item => item.Key, item => item.Value)
                            .FirstOrDefault();
                if(x.Key != null)
                {
                    Console.WriteLine($"{x.Value}"); //this should return "SELECT * FROM ORDER"
                }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号