我如何在ConcurrentDictionary上编写扩展方法
本文关键字:扩展 方法 ConcurrentDictionary | 更新日期: 2023-09-27 18:02:50
我有一个ConcurrentDictionary对象定义为ConcurrentDictionary<int, DirtyFlag><int, DirtyFlag>
,其中dirty标志是一个具有dirty/Clean值的Enum。
我正试图写一个扩展方法,只获得脏对象。那些将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.
帮忙吗?
public static ConcurrentDictionary<int, DirtyFlag> GetDirtyRoutes(this ConcurrentDictionary<int, DirtyFlag> wholeDictionary)
{
return new ConcurrentDictionary<int, DirtyFlag>(
wholeDictionary.Where(a => a.Value == DirtyFlag.Dirty)
);
}