如何将项目列表转换为列表<;项目>;在F#
尝试做一些非常基本的事情——将F#列表转换为.Net通用列表(我想) 我是新来的 我想这是F型的?我需要设法让它成为一个如何将项目列表转换为列表<;项目>;在F#,f#,F#,尝试做一些非常基本的事情——将F#列表转换为.Net通用列表(我想) 我是新来的 我想这是F型的?我需要设法让它成为一个 List<Item> 列表 感谢您的时间首先,这里的命名有点混乱,因为F#定义了自己的列表类型(这是一个不可变的列表),可以写成列表 如注释中所述,您可以使用构造函数将序列转换回ResizeArray。但是,泛型.NET列表类型也直接支持map操作,只是它是一个名为ConvertAll的实例方法。您可以使用它,或者编写一个更为F#友好的包装: module
List<Item>
列表
感谢您的时间首先,这里的命名有点混乱,因为F#定义了自己的
列表
类型(这是一个不可变的列表),可以写成列表
如注释中所述,您可以使用构造函数将序列转换回ResizeArray
。但是,泛型.NET列表类型也直接支持map
操作,只是它是一个名为ConvertAll
的实例方法。您可以使用它,或者编写一个更为F#友好的包装:
module ResizeArray =
let map f (l:ResizeArray<_>) = l.ConvertAll(System.Converter(f))
要将F#list转换为System.Collections.Generic.list,请使用System.Linq.ToList或仅使用构造函数System.Collections.Generic.list
open System.Linq
open System.Collections.Generic
let l = [1..3] // F# lists implement IEnumerable<'t>, so we can use
l.ToList() // System.Linq.ToList method
l |> List // System.Collections.Generic.List constructor
opensystem.Linq
open System.Collections.Generic
让l=[1..3]//F#列表实现IEnumerablueResizeArray=System.Collections.Generic.List
isMicrosoft.FSharp.Collections.List出于好奇,只需将|>Seq.ToList
替换为| ResizeArray
,为什么需要将数据保存在System.Collections.Generic.List
collection中?@TomasPetricek它是一个C#library(Xero)中的类型集合,所以我需要将它以那种格式推回到API中。@AlbertoLeón谢谢。我没有测试过它,但它似乎接受了它,尽管它的类型略有不同。|>这个列表似乎也很有效,我会把它们都测试出来。天才-谢谢!
module ResizeArray =
let map f (l:ResizeArray<_>) = l.ConvertAll(System.Converter(f))
let items = pricelistItems |> ResizeArray.map (fun pli ->
Item(Code = pli.sku,
Description = "",
IsPurchased = true,
IsSold = true,
IsTrackedAsInventory = true,
InventoryAssetAccountCode = "xxxx") )
open System.Linq
open System.Collections.Generic
let l = [1..3] // F# lists implement IEnumerable<'t>, so we can use
l.ToList() // System.Linq.ToList method
l |> List // System.Collections.Generic.List constructor
let items =
pricelistItems // List<PriceListItem>
|> Seq.map(fun pli ->
let item =
new Item(
Code = pli.sku,
Description = "",
IsPurchased = true,
IsSold = true,
IsTrackedAsInventory = true,
InventoryAssetAccountCode = "xxxx"
)
item
)
|> System.Linq.Enumerable.ToList
open System.Collections.Generic // List
let items =
pricelistItems // List<PriceListItem>
|> Seq.map(fun pli ->
let item =
new Item(
Code = pli.sku,
Description = "",
IsPurchased = true,
IsSold = true,
IsTrackedAsInventory = true,
InventoryAssetAccountCode = "xxxx"
)
item
)
|> List // constructor accepting IEnumerable = the sequence above
[1..3] |> List
[1..3] |> List<_>
[1..3] |> List<int>