PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2015 Getting sum of database-VBForums
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
    5,630

    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


    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

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,619

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width