[英]Using C# Linq ForEach() in F#
I'm translating some code from C# to F#, and I have the following lines that I need to cross over the F#: 我正在将一些代码从C#转换为F#,并且我需要跨越F#的以下几行:
List<VirtualMachine> vmList = new List<VirtualMachine>();
m_vimClient.FindEntityViews(typeof(VirtualMachine), null, null, null).ForEach(vm => vmList.Add((VirtualMachine)vm));
return vmList;
I did the following: 我做了以下工作:
let vmList = vimClient.FindEntityViews(typedefof<VirtualMachine>, null, null, null).ForEach(vm => vmList.Add((VirtualMachine)vm))
vmList
Unfortunately, Intellisense is telling me that vm
and vmList
are not defined in the ForEach()
part of the F# code. 不幸的是,Intellisense告诉我vm
和vmList
没有在F#代码的ForEach()
部分中定义。
How would I solve this problem? 我该如何解决这个问题?
The lambda syntax you used is C# syntax. 您使用的lambda语法是C#语法。 In F#, a lambda is defined like fun vm -> …
. 在F#中,lambda的定义类似于fun vm -> …
。
That said, you don't need the ForEach
at all. 也就是说,您根本不需要ForEach
。 The C# version could be written without the lambda as: C#版本可以不用lambda编写为:
var vmList = m_vimClient.FindEntityViews(typeof(VirtualMachine), null, null, null)
.Cast<VirtualMachine>()
.ToList();
In F#, this would be: 在F#中,这将是:
let vmList = m_vimClient.FindEntityViews(typedefof<VirtualMachine>, null, null, null)
|> Seq.cast<VirtualMachine>
|> Seq.toList
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.