从 C # 通用字典中过滤出值

我有一个 C # 字典,Dictionary<Guid, MyObject>,我需要根据 MyObject的属性进行过滤。

例如,我想从字典中删除 MyObject.BooleanProperty = false所在的所有记录。实现这一目标的最佳方法是什么?

117234 次浏览

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.

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.

You can simply use the Linq where clause:

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

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

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"
}