Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 29th, 2010, 10:18 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default SUM LINQ to Objects

Below is code that will place 2 controls on the form. And Build a class.
When the button is clicked it will fill the listbox with the contents of the class, and count the number of entries... I need to know how to sum the amounts.
Code:
 Public Class Form1
    Dim btn As New Button
    Dim l1 As New ListBox
    Dim b As New Bill
    Dim lB As New List(Of Bill)


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        btn = New Button
        btn.Text = "Click To Aggregate"
        btn.Size = New Size(160, 140)

        Me.Controls.Add(btn)


        AddHandler btn.Click, New System.EventHandler(AddressOf BTN_Click)
        l1 = New ListBox
        l1.Size = New Size(200, 200)
        l1.Location = New Point(140, 140)

        Me.Controls.Add(l1)
        BuildBill()
    End Sub
    Public Sub BuildBill()
        b = New Bill
        b.Name = "ME     "
        b.Amount = "2"
        lB.Add(b)
        b = New Bill
        b.Name = "You    "
        b.Amount = "4"
        lB.Add(b)

        b = New Bill
        b.Name = "Them  "
        b.Amount = "5"
        lB.Add(b)

        b = New Bill
        b.Name = "Us      "
        b.Amount = "3"
        lB.Add(b)
    End Sub
    Public Sub BTN_Click()
        Dim query = From gg In lB
        For Each gg In query
            l1.Items.Add(gg.Name & gg.Amount)

        Next
        l1.Items.Add("There Are " & query.Count & " Entries")
        MsgBox("How Would I Aggregate This Class to give me the sum of the Amounts?")
    End Sub
    <Serializable()> Public Class Bill
        Private _Name As String
        Private _Amount As String

        Property Name As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
       Property Amount As String
            Get
                Return _Amount
            End Get
            Set(ByVal value As String)
                _Amount = value
            End Set
        End Property
    End Class
End Class
nothing I've looked at in helps will compile...
 
Old July 31st, 2011, 09:46 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Aggregate Function Simplified!

Dim f = (From gg In query Select gg.Amount).Sum


This Will Work Fine If gg.Amount Is Declared as An Integer Or A Double.

If It IS Declared as a string you will get the following error:

1 Overload resolution failed because no accessible 'Sum' accepts this number of arguments ...

The Solution ? Either change the delcaration to An INteger Or Double or ...
Change the LINQ Statement to
Dim f = (From gg In query Select cDbl(gg.Amount)).Sum





Similar Threads
Thread Thread Starter Forum Replies Last Post
LINQ vaibhavgoe LINQ 4 December 21st, 2009 03:49 PM
Pivot data: Linq to Objects jimibt BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 22nd, 2008 01:22 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
Automatic Sum for Diff Objects in the same sheet sedolphi Excel VBA 0 May 21st, 2004 02:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.