Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#使用IDictionaryEnumerator_F#_C# To F# - Fatal编程技术网

F#使用IDictionaryEnumerator

F#使用IDictionaryEnumerator,f#,c#-to-f#,F#,C# To F#,因此,我正在使用HttpRuntime.cache,它返回一个IDictionaryEnumerator作为其“枚举器”,它似乎与List或Seq不兼容,因此我能找到的唯一使用它的方法是下拉到不神圣的命令代码中,如下所示: member this.GetCountStartsWith keyPrefix = let enumerator = HttpRuntime.Cache.GetEnumerator() le

因此,我正在使用HttpRuntime.cache,它返回一个IDictionaryEnumerator作为其“枚举器”,它似乎与List或Seq不兼容,因此我能找到的唯一使用它的方法是下拉到不神圣的命令代码中,如下所示:

 member this.GetCountStartsWith keyPrefix =
            let enumerator = HttpRuntime.Cache.GetEnumerator()                 
            let mutable counter = 0
            while enumerator.MoveNext() do
                match enumerator.Key.ToString().StartsWith(keyPrefix) with
                | true -> counter <- counter + 1
                | false -> ()
            counter
member this.GetCountStartsWith keyPrefix=
让enumerator=HttpRuntime.Cache.GetEnumerator()
设可变计数器=0
当枚举器.MoveNext()执行以下操作时
将enumerator.Key.ToString().StartsWith(keyPrefix)与匹配
|真->计数器()
柜台

我曾想过可以创建一个helper函数来将其转储到列表中,但可能会有数千个元素,因此从性能角度来看,这是一个坏主意。有没有办法用更惯用的方式来处理这个问题

问题是您有一个
IEnumerator
,它不是通用的。但是,有两种辅助方法可以解决这个问题:
of type
Cast

实际上,您希望执行以下操作:

let enumerable = HttpRuntime.Cache |> Seq.cast<DictionaryEntry>
let enumerable=HttpRuntime.Cache |>Seq.cast

现在,
enumerable
是一个合适的通用enumerable:)

@Carsten好吧,看看写着
public sealed class Cache:IEnumerable
:)的部分,它确实可以编译,但我实际上还没有为web开发设置F,所以我无法在运行时检查它。。。我可以发誓在MSDN上曾经有一个带有接口的部分(当然它就在explicite下)。。。抱歉,我不认为你的代码完全正确,但它和一个现在已删除的答案让我找到了解决方案:
HttpRuntime.Cache |>Seq.cast
@jackmott是的,我在F方面还是个新手-我写的是如何在C中解决同样的问题
Seq
只是
IEnumerable
的别名,但我忘了F#不处理扩展方法:D让我在示例中使用显式调用……酷,我接受了你的答案。无论是谁发布了另一个部分解决方案,被否决,然后被删除,我爱你。