Hi all, I receive an array of classes from a service that contains properties such as phone and call time. This list could have multiple phone numbers, as calls may be attempted more than once.
I'd like to only have a list with the most recent attempt for each number, or, a list of the most recent item within a group of items contained in an array. Since I can't modify the service, I'll have to do this within VB.
Here's what I was thinking, since I can arrange to have the array sorted by phone asc, calltime desc. Create a different array, and add items to it while looping through the initial array:
Dim someString() as ClassName = FunctionThatReturnsArrayOfClassNames
Dim otherString() as ClassName
For i as Int32 = 0 to someString.length - 1
If Not otherString.Contains(someString(i)) then
'Add
Else
End If
Next
Seems like that would work, but I wondered if this method would be efficient, or if there is a more efficient way? Looks like I'd have to use Redim and Preserve, and I've read that method is inefficient. Or maybe I'm completely wrong, and it won't even
work - about to try right now.
Imports Microsoft.VisualBasic
Public Class PhoneCalls
Dim _phoneno As String
Dim _calltime As Date
Public Property phoneno() As String
Get
Return _phoneno
End Get
Set(value As String)
_phoneno = value
End Set
End Property
Public Property calltime() As Date
Get
Return _calltime
End Get
Set(value As Date)
_calltime = value
End Set
End Property
Sub New()
End Sub
Sub New(_phoneno As String, _calltime As Date)
phoneno = _phoneno
calltime = _calltime
End Sub
End Class
Imports PhoneCalls
Imports System.Linq
Partial Class Default1
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim pc As PhoneCalls() = {New PhoneCalls("123", Date.Now), New PhoneCalls("456", Date.Now.AddDays(-1)),
New PhoneCalls("123", Date.Now.AddDays(1))}
Dim pc1 As List(Of PhoneCalls)
pc1 = pc.ToList()
Dim query = From p In pc1
Group p By p.phoneno Into g = Group
Select New With {g, .calltime = g.Max(Function(p) p.calltime)}
Dim str As String = ""
For Each q In Query
str += " " + q.g.First(Function(p) p.phoneno).phoneno.ToString() + " - " + q.calltime.ToString()
Next
End Sub
End Class
carriehoff
Member
37 Points
32 Posts
Select most recent items from array of classes
May 17, 2012 07:53 PM|LINK
Hi all, I receive an array of classes from a service that contains properties such as phone and call time. This list could have multiple phone numbers, as calls may be attempted more than once.
I'd like to only have a list with the most recent attempt for each number, or, a list of the most recent item within a group of items contained in an array. Since I can't modify the service, I'll have to do this within VB.
Here's what I was thinking, since I can arrange to have the array sorted by phone asc, calltime desc. Create a different array, and add items to it while looping through the initial array:
Dim someString() as ClassName = FunctionThatReturnsArrayOfClassNames Dim otherString() as ClassName For i as Int32 = 0 to someString.length - 1 If Not otherString.Contains(someString(i)) then 'Add Else End If NextSeems like that would work, but I wondered if this method would be efficient, or if there is a more efficient way? Looks like I'd have to use Redim and Preserve, and I've read that method is inefficient. Or maybe I'm completely wrong, and it won't even work - about to try right now.
Thanks for any help,
Carrie
tusharrs
Contributor
3230 Points
668 Posts
Re: Select most recent items from array of classes
May 18, 2012 09:08 AM|LINK
use linq for your solution
In str string variable your result for unique phone nos and their last call timings
also refer to this http://msdn.microsoft.com/en-us/library/bb386922.aspx#Y272
class
Imports Microsoft.VisualBasic Public Class PhoneCalls Dim _phoneno As String Dim _calltime As Date Public Property phoneno() As String Get Return _phoneno End Get Set(value As String) _phoneno = value End Set End Property Public Property calltime() As Date Get Return _calltime End Get Set(value As Date) _calltime = value End Set End Property Sub New() End Sub Sub New(_phoneno As String, _calltime As Date) phoneno = _phoneno calltime = _calltime End Sub End ClassImports PhoneCalls Imports System.Linq Partial Class Default1 Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim pc As PhoneCalls() = {New PhoneCalls("123", Date.Now), New PhoneCalls("456", Date.Now.AddDays(-1)), New PhoneCalls("123", Date.Now.AddDays(1))} Dim pc1 As List(Of PhoneCalls) pc1 = pc.ToList() Dim query = From p In pc1 Group p By p.phoneno Into g = Group Select New With {g, .calltime = g.Max(Function(p) p.calltime)} Dim str As String = "" For Each q In Query str += " " + q.g.First(Function(p) p.phoneno).phoneno.ToString() + " - " + q.calltime.ToString() Next End Sub End Class( Mark as Answer if it helps you out )
View my Blog