如何在 F# 中将项目列表转换为列表<Item>
How to convert Item list to List<Item> in F#
尝试做一些非常基本的事情 - 将 F# 列表转换为 .Net 通用列表(我认为)
我是 F# 新手。
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
)
|> Seq.toList
上面代码的结果类型是
Item list
我认为这是 F# 类型?我需要以某种方式让它成为
List<Item>
感谢您的宝贵时间
首先,这里的命名有点混乱,因为F#定义了自己的List
类型(即不可变列表),可以写成List<'T>
、'T List
或 list<'T>
或 'T list
。这些都参考了F#列表。要引用标准通用 .NET 类型,F# 使用 ResizeArray<'T>
或者您可以使用 System.Collections.Generic.List<'T>
的完全限定名称
如评论中所述,您可以使用构造函数将序列转换回 ResizeArray
。然而,泛型.NET 列表类型也支持直接map
操作,只是它是一个名为ConvertAll
的实例方法。您可以使用它或编写对 F# 更友好的包装器:
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") )
要将 F# 列表转换为 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
你的具体密码是
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
您还可以将序列(即 IEnumerable<>)传递给 System.Collections.Generic.List 构造函数:
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
请注意,上面的“List”实际上是 List 您也可以键入 List<_> 并让编译器推断类型甚至插入您的类型。我更喜欢最短的方法,而忽略了编译器接受的泛型。这些是等价的:
[1..3] |> List
[1..3] |> List<_>
[1..3] |> List<int>
尝试做一些非常基本的事情 - 将 F# 列表转换为 .Net 通用列表(我认为)
我是 F# 新手。
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
)
|> Seq.toList
上面代码的结果类型是
Item list
我认为这是 F# 类型?我需要以某种方式让它成为
List<Item>
感谢您的宝贵时间
首先,这里的命名有点混乱,因为F#定义了自己的List
类型(即不可变列表),可以写成List<'T>
、'T List
或 list<'T>
或 'T list
。这些都参考了F#列表。要引用标准通用 .NET 类型,F# 使用 ResizeArray<'T>
或者您可以使用 System.Collections.Generic.List<'T>
如评论中所述,您可以使用构造函数将序列转换回 ResizeArray
。然而,泛型.NET 列表类型也支持直接map
操作,只是它是一个名为ConvertAll
的实例方法。您可以使用它或编写对 F# 更友好的包装器:
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") )
要将 F# 列表转换为 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
你的具体密码是
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
您还可以将序列(即 IEnumerable<>)传递给 System.Collections.Generic.List 构造函数:
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
请注意,上面的“List”实际上是 List 您也可以键入 List<_> 并让编译器推断类型甚至插入您的类型。我更喜欢最短的方法,而忽略了编译器接受的泛型。这些是等价的:
[1..3] |> List
[1..3] |> List<_>
[1..3] |> List<int>