Solved

Selecting & Sorting a Generic List of Custom Objects

Posted on 2014-04-02
5
242 Views
Last Modified: 2016-02-16
Hi,

I have a Generic Collection of a custom type that I have created. Short of using a LINQ query  (which I have never used before), I need to sort the collection based on an individual element (a time stamp) so I can retrieve the newest data added to the collection for a given UserID.

    Structure AuthenticationRecord
        Dim UserID As String
        Dim UserPW As String
        Dim AuthLevel As String
        Dim TS As Date
    End Structure

    Dim AuthenticationDataCache As List(Of AuthenticationRecord)

    Private Sub Test()

        Dim AR As AuthenticationRecord

        AR.UserID = "TestUserName1"
        AR.UserPW = "TestPW1"
        AR.AuthLevel = "3"
        AR.TS = Now.AddMilliseconds(-1000)
        AuthenticationDataCache.Add(AR)

        AR.UserID = "TestUserName1"
        AR.UserPW = "TestPW1"
        AR.AuthLevel = "-1"
        AR.TS = Now()
        AuthenticationDataCache.Add(AR)

        'Here is where I get stuck....
        'My need is to retrieve the newest Record in the collection (as defined by TS). In the above
        'example, the record with AuthLevel = -1 is most recent (based on TS)
        'I either need a LINQ Query to do this, or to sort the contents of the collection
        'with the most recent TS record item towards the top, then I would iterate through the collection
        'from the beginning to find the first occuring UserID I was looking for.


    End Sub

Open in new window

0
Comment
5 Comments
 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 50 total points
ID: 39974294
Dim sortedCollection = From p In AuthenticationDataCacheOrder By p.TS Descendingp

'Collection now would be sorted.
'After this you can iterate either through For or For Each loop to get the first element
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39975301
Hi RobertFromSecretWeapons;

Using Linq will be the better way to go here. Using @ jayakrishnabh solution correcting a couple of typos.

Dim sortedCollection = From p In AuthenticationDataCache
                       Order By p.TS Descending

Open in new window

0
 

Author Comment

by:RobertFromSecretWeapons
ID: 39976543
Well, heck, that was simple and nice.

Assuming that the collection would have many instances of same AND different UserID's, could I extend that LINQ query to sort first and then retrieve whatever UserID I was looking for (being the most recent record added to the collection as specified by TS.  That's ultimately what I need.

That would be super nice.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 39976641
Hi RobertFromSecretWeapons;

The following query will return one Latest record for the user "TestUserName1"

Dim lookingFor As String = "TestUserName1"

Dim sortedCollection = From p In AuthenticationDataCache
                       Group p By Key = p.UserID Into Group
                       Where Key = lookingFor
                       Select Group.OrderByDescending(Function(t) t.UserID).FirstOrDefault()

Open in new window

0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 39976758
You don't *have* to use LINQ for this. The List class defines a Sort method, one overload of which takes in a Comparison delegate--which means it also accepets an anonymous function.

e.g.

AuthenticationDataCache.Sort(Function (left, right) left.TS.CompareTo(right.TS))

Open in new window


You would then have the list in ascending order, so just grab the last element. If you'd rather do descending order, so you can grab the first element (rather than the last), you can simply switch "right" and "left" in the anonymous function body:

AuthenticationDataCache.Sort(Function (left, right) right.TS.CompareTo(left.TS))

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now