我如何在ConcurrentDictionary上编写扩展方法

时间:2011-07-20 20:31:00

标签: c# c#-4.0 extension-methods

我有一个ConcurrentDictionary对象,定义为ConcurrentDictionary<int, DirtyFlag><int, DirtyFlag>,其中脏标志是一个具有Dirty / Clean值的Enum。

我正在尝试编写一个扩展方法来获取Dirty对象。将DirtyFlag设置为Dirty的那些对象。

我尝试过使用它,但它给了我一个错误:

public static ConcurrentDictionary<int, DirtyFlag> GetDirtyRoutes(this ConcurrentDictionary<int, DirtyFlag> wholeDictionary)
{

    return wholeDictionary.SelectMany(a => a.Value == DirtyFlag.Dirty);

}

这是我收到的错误消息:

The type arguments for method 'System.Linq.Enumerable.SelectMany<TSource,TResult>
(System.Collections.Generic.IEnumerable<TSource>, 
System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>)' cannot 
be inferred from the usage. Try specifying the type arguments explicitly.

任何帮助?

1 个答案:

答案 0 :(得分:2)

public static ConcurrentDictionary<int, DirtyFlag> GetDirtyRoutes(this ConcurrentDictionary<int, DirtyFlag> wholeDictionary)
{
    return new ConcurrentDictionary<int, DirtyFlag>(
        wholeDictionary.Where(a => a.Value == DirtyFlag.Dirty)
    );
}