Solved

Aggregating Objects in Collection

Posted on 2012-03-27
3
284 Views
Last Modified: 2012-04-03
I have a custom collection that inherits from List(of T) that is used to store objects that expose item no, qty and price properties.

What I want to do is summarise the quantities in a data grid based on item no and price so for example if my objects hold the following information for item no, qty and price:

a, 1, 10.00
a, 1, 10.00
b, 1, 20.00
a, 1, 50.00
a, 1, 10.00

I would show the following in the grid

a, 3, 10.00
a, 1, 50.00
b, 3, 20.00

What's the best way to do this in VB.Net? I've been looking at the Group By in LINQ but most examples are in C# and I'm struggling to get things to work.
0
Comment
Question by:monserob
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37771451
See if this works for you. Please feel free to ask further questions if you require any clarification  = )

Module Module1

    Sub Main()
        Dim aList As New List(Of ObjectsWhichExpose)

        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 10.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 10.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "b", .Quantity = 1, .Price = 20.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 50.0})
        aList.Add(New ObjectsWhichExpose() With {.ItemNo = "a", .Quantity = 1, .Price = 10.0})

        Dim query = From item In aList _
                    Group item By item.ItemNo, item.Price Into Group _
                    Order By ItemNo _
                    Select New With {.ItemNo = ItemNo, .Quantity = Group.Sum(Function(x As ObjectsWhichExpose) x.Quantity), .Price = Price}

        For Each item In query
            Console.WriteLine("{0}, {1}, {2}", item.ItemNo, item.Quantity, item.Price)
        Next

        Console.ReadKey()
    End Sub

End Module

Public Class ObjectsWhichExpose


    Private _itemNo As String
    Public Property ItemNo() As String
        Get
            Return _itemNo
        End Get
        Set(ByVal value As String)
            _itemNo = value
        End Set
    End Property


    Private _qty As Integer
    Public Property Quantity() As Integer
        Get
            Return _qty
        End Get
        Set(ByVal value As Integer)
            _qty = value
        End Set
    End Property


    Private _price As Double
    Public Property Price() As Double
        Get
            Return _price
        End Get
        Set(ByVal value As Double)
            _price = value
        End Set
    End Property


End Class

Open in new window

0
 

Author Comment

by:monserob
ID: 37771959
That's great thanks!

Before I mark as accepted can I ask whether there's an easy of associating the grouped objects with the underlying objects in the collection e.g. if the user was to delete a summary row in the grid I would need to find the individual objects that were grouped on that row and remove them from the collection. Would this be another LINQ to Object?
0
 

Author Comment

by:monserob
ID: 37801766
I've not received a response, which is a pity but in fairness a solution to the original question has been provided so I will mark as accepted.

If I run into difficulties with removing the objects that belong to a summarised row I'll post a new question
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question