Query .NET Collection

With the inclusion of LINQ in .NET querying the collection has become easy like never before. We can find MAX, MIN, AVERAGE, SUM from the collection without using loops. We can also search as well as Order the collection as we want. Below is the code for some general operations we use to do before using sql queries or had to right loops.

Code for Customer Class


Public Class Customer

Public Sub New(ByVal customerId As String, ByVal customerName As String, ByVal customerAge As Integer)
mCustomerID = customerId
mCustomerName = customerName
mCustomerAge = customerAge
End Sub

Private mCustomerID As String
Private mCustomerName As String
Private mCustomerAge As Integer

Public Property CustomerId() As String
Get
Return mCustomerID
End Get
Set(ByVal value As String)
mCustomerID = value
End Set
End Property

Public Property CustomerName() As String
Get
Return mCustomerName
End Get
Set(ByVal value As String)
mCustomerName = value
End Set
End Property

Public Property CustomerAge() As Integer
Get
Return mCustomerAge
End Get
Set(ByVal value As Integer)
mCustomerAge = value
End Set
End Property
End Class






Implementation of various methods


Imports System.Net
Imports System.Text


Public Class Form1

Dim mCustomers As New System.Collections.ObjectModel.Collection(Of Customer)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mCustomers.Add(New Customer("A002", "Sunil Kapoor", 12))
mCustomers.Add(New Customer("A003", "Pankaj Patel", 40))
mCustomers.Add(New Customer("A002", "Jim Shah", 24))
End Sub

''' Will return Maximum Customer ID from the collection
Private Sub btnMax_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMax.Click
Dim result As Integer = mCustomers.Max(Function(item) item.CustomerId.ToString.Substring(1))
MsgBox("Maximum Customer Id : = " & result)
End Sub


''' Will return Minimum Customer ID from the collection
Private Sub btnMin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMin.Click
Dim result As Integer = mCustomers.Min(Function(item) item.CustomerId.ToString.Substring(1))
MsgBox("Minimum Customer Id : = " & result)
End Sub

''' Will Return Sum of Customer Age in the Collection
Private Sub btnSum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSum.Click
Dim result As Integer = mCustomers.Sum(Function(item) item.CustomerAge)
MsgBox("Sum of Customers Age : = " & result)
End Sub


''' Will return Average Age of Customer from the Collection
Private Sub btnAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAverage.Click
Dim result As Integer = mCustomers.Average(Function(item) item.CustomerAge)
MsgBox("Average age of Customers : = " & result)
End Sub

''' Return all the Customer Name with age less than 25
Private Sub btnWhere_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWhere.Click
Dim result As IEnumerable(Of Customer) = mCustomers.Where(Function(item) item.CustomerAge < 25)
Dim str As New StringBuilder
str.AppendLine("List of Customers with age less than 25 are:")
For i As Integer = 0 To result.Count - 1
str.AppendLine(result(i).CustomerName)
Next
MsgBox(str.ToString)
End Sub

''' Return customers order by name
Private Sub btnOrderBy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrderBy.Click
Dim result As IEnumerable(Of Customer) = mCustomers.OrderBy(Function(item) item.CustomerName)
Dim str As New StringBuilder
str.AppendLine("List of Customers order by name are :")
For i As Integer = 0 To result.Count - 1
str.AppendLine(result(i).CustomerName)
Next
MsgBox(str.ToString)
End Sub
End Class


Comments

Author: Fool To Code11 Nov 2009 Member Level: Gold   Points : 0

cooool resource dude



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: