Solved

Selecting & Sorting a Generic List of Custom Objects

Posted on 2014-04-02
5
243 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 75

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

948 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

23 Experts available now in Live!

Get 1:1 Help Now