VB.NET LINQ查询:获取特定结构成员的所有值的总和 [英] VB.NET LINQ Query: Getting The Sum of All Values For A Specific Structure Member

查看:66
本文介绍了VB.NET LINQ查询:获取特定结构成员的所有值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET中,假设我具有以下结构:

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 Decimal
End Structure

...以及产品的一般列表:

... and a Generic List of the Products:

Dim ProductsList As New List(Of Product)

Dim product1 As New Product

With product1
    .ItemNo = 100
    .Description = "Standard Widget"
    .Cost = 10D
End With

ProductsList.Add(product1)

Dim product2 As New Product

With product2
    .ItemNo = 101
    .Description = "Standard Cog"
    .Cost = 10.95D
End With

ProductsList.Add(product2)

Dim product3 As New Product

With product3
    .ItemNo = 101
    .Description = "Industrial Strenght Sprocket"
    .Cost = 99.95D
End With

ProductsList.Add(product3)

我将如何定义LINQ查询以对列表中的所有Product.Cost值求和?换句话说,什么是LINQ 在VB.NET中查询以返回值120.90,该值反映了单个LINQ查询中所有三个产品成本值的总和?

How would I define a LINQ Query to Sum all of the Product.Cost values in the List? In other words, what would be the LINQ Query in VB.NET to return the value 120.90, which reflects the sum of all three Product Cost values in a single LINQ Query?

推荐答案

内置的Sum方法已经做到了.

The built in Sum method does this already.

在VB中,它看起来像这样:

In VB it looks like this:

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

在C#中,它看起来像这样:

In C# it looks like this:

ProductsList.Sum( (item) => item.Cost);

这篇关于VB.NET LINQ查询:获取特定结构成员的所有值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆