Results 1 to 3 of 3

Thread: Getting sum of database

  1. #1

    Thread Starter
    New Member tryingtoohard's Avatar
    Join Date
    Dec 2017
    Posts
    1

    Getting sum of database

    I am trying to write a program that will be able to sum up a total from a database file and am stuck. I've tried a few different ideas and nothing has worked so far. This is what I currently have

    Code:
    Dim SumQuery = From popualtion In PopulationDBDataSet.City
                           Aggregate population In PopulationDBDataSet.City
                               Into Sum(population)
    I keep getting the error "method sum is not accessible in this context". Has anyone else had this happen to them or have any suggestions?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Getting sum of database

    I am assuming you want to sum the populations of all cities. I don't know how your classes are defined out so I mocked up an example of a similar problem. Maybe you can use it to help you:-
    vbnet Code:
    1. '
    2. Public Class Form1
    3.  
    4.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         Dim cities As New List(Of City)
    7.  
    8.         cities.Add(New City("New York", 9092))
    9.         cities.Add(New City("POS", 3443))
    10.         cities.Add(New City("Kio", 9011))
    11.         cities.Add(New City("Loinitte", 4033))
    12.  
    13.         'Sum the populations of all cities
    14.         Dim sumOfPop As Integer = Aggregate city In cities Into Sum(city.Population)
    15.  
    16.  
    17.  
    18.     End Sub
    19. End Class
    20.  
    21.  
    22. Public Class City
    23.  
    24.     Public Sub New(ByVal cityName As String, ByVal pop As Integer)
    25.         Me.CityName = cityName
    26.         Me.Population = pop
    27.     End Sub
    28.  
    29.     Public Sub New()
    30.  
    31.     End Sub
    32.  
    33.     Public Property CityName As String
    34.     Public Property Population As Integer
    35.  
    36. End Class
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Getting sum of database

    I'm not seeing a reason that it wouldn't just be like this:
    vb.net Code:
    1. Dim SumQuery = PopulationDBDataSet.City.AsEnumerable().Sum(Function(c) c.Population)
    If this is a typed DataSet - which it appears to be - then I don't think that the AsEnumerable is required.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width