在VB.NET中,我们假设我有以下结构:
Public Structure Product Public ItemNo As Int32 Public Description As String Public Cost As Decimal End Structure
…和产品的通用列表:
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 Query中所有三个Product Cost值的总和?
内置Sum方法已经这样做了.
在VB中,它看起来像这样:
ProductList.Sum(Function(item) item.Cost)
在C#中,它看起来像这样:
ProductsList.Sum( (item) => item.Cost);