问题
Suppose I have the following record type:
open System.Collections.Generic
type Store =
{
Products: List<Product>;
}
member this.TotalNumberOfItems =
this.Products.Sum(fun p -> p.NumberInInventory)
I want to sum a count of total items in the store as in the method above, however the System.Collections.Generic.List extension methods don't seem to be available. Intellisense does not show them. How can I call Sum from F#?
回答1:
You just need to open the System.Linq
namespace. For example:
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)
来源:https://stackoverflow.com/questions/17286953/how-do-i-use-the-extension-method-sum-on-a-net-list-in-f