0

假设我有以下记录类型:

open System.Collections.Generic

type Store =
    {
        Products: List<Product>;
    }
    member this.TotalNumberOfItems =
        this.Products.Sum(fun p -> p.NumberInInventory)

我想按照上述方法计算商店中的总商品数量,但是 System.Collections.Generic.List 扩展方法似乎不可用。Intellisense 不会显示它们。如何从 F# 调用 Sum?

4

1 回答 1

2

您只需要打开System.Linq命名空间。例如:

open System.Collections.Generic
open System.Linq

type Product = { NumberInInventory : int; }

type Store =
    {
        Products: List<Product>;
    }
    member this.TotalNumberOfItems =
        this.Products.Sum(fun p -> p.NumberInInventory)
于 2013-06-24T23:52:46.560 回答