How do I use the extension method Sum on a .NET list in F#?

安稳与你 提交于 2019-12-11 14:03:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!