VB.NET LINQ Query: Getting The Sum of All Values For A Specific Structure Member

后端 未结 2 1200
长情又很酷
长情又很酷 2021-02-18 16:53

In VB.NET, let\'s assume I have the following Structure:

Public Structure Product
    Public ItemNo As Int32
    Public Description As String
    Public Cost As          


        
相关标签:
2条回答
  • 2021-02-18 17:26

    The built in Sum method does this already.

    In VB it looks like this:

    ProductList.Sum(Function(item) item.Cost)
    

    In C# it looks like this:

    ProductsList.Sum( (item) => item.Cost);
    
    0 讨论(0)
  • 2021-02-18 17:32

    One way would be:

    Dim s = (From p As Product In products Select p.Cost).Sum()
    
    0 讨论(0)
提交回复
热议问题