Solved

Sorting a System.Collections.Generic List

Posted on 2014-03-21
4
332 Views
Last Modified: 2014-03-21
I need to be able to sort a list collection based off one of two elements within the list depending on a radio button selection on my page (username& EmpID) prior to loading the list into a drop down list control.  Any assistance would be greatly appreciated!

Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Collections.Generic
Imports System.Net
Imports System.Text
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.Data.Common
Imports Teradata.Net.Security
Imports Teradata.Client.Provider
Imports Teradata.Client.Provider.TdConnection
Imports System.Data.Common.DbConnectionStringBuilder
Imports System.Data.Common.DbTransaction
Imports System.Security.AccessControl
Imports System.Security.Principal
Imports System.Net.Mail

Partial Class Alter
  Inherits System.Web.UI.Page
  Dim newUser As userInfo
  Dim lstUsers As List(Of userInfo)

#Region " Control Events "
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
      PullUserList()
      Session("page") = 4
    End If

  End Sub
#End Region

#Region " Pull User List "
  Public Sub PullUserList()
    Dim SelStmt, rsCommString As String
    Dim pf As DbProviderFactory = DbProviderFactories.GetFactory("Teradata.Client.Provider")
    Dim con As DbConnection = pf.CreateConnection()
    Dim command As DbCommand = con.CreateCommand
    Dim stringBuilder As New Teradata.Client.Provider.TdConnectionStringBuilder()
    Dim SqlQuery As StringBuilder = New StringBuilder
    Dim arPtr As Integer
    lstUsers = New List(Of userInfo)()

    stringBuilder.DataSource = Session.Contents("DataSource")
    stringBuilder.UserId = Session.Contents("AdminLogin")
    stringBuilder.Password = Session.Contents("AdminPasswd")
    stringBuilder.PersistSecurityInfo = True

    con.ConnectionString = stringBuilder.ConnectionString

    'Get Master List of Users
    SqlQuery.Clear()
    SqlQuery.Append("Sel * From TABLE")
    SelStmt = SqlQuery.ToString()

    command.CommandText = SelStmt
    command.CommandType = CommandType.Text
    command.CommandTimeout = 600

    Try
      con.Open()
      Dim reader As DbDataReader = command.ExecuteReader()


      Do While reader.Read()
        If reader.HasRows Then
          newUser = New userInfo()
          If IsDBNull(reader("commentstring").ToString) = False Then
            rsCommString = reader("commentstring").ToString
          Else
            rsCommString = ""
          End If

          If InStr(rsCommString, "~") <> 0 And _
             InStr(rsCommString, "#") <> 0 And _
             InStr(rsCommString, "$") <> 0 And _
             InStr(rsCommString, "*") <> 0 And _
             InStr(rsCommString, "^") <> 0 And _
             InStr(rsCommString, "+") <> 0 Then

            newUser.username = UCase(reader("username").ToString)
            newUser.comment = reader("commentstring").ToString
            newUser.ownerName = reader("ownername").ToString


            If InStr(UCase(rsCommString), "CONSULTANT") > 0 Then
              newUser.EmpID = ""
            Else
              If InStr(rsCommString, "*") - InStr(rsCommString, "$") > 1 Then
                newUser.EmpID = Mid$(rsCommString, (InStr(rsCommString, "$") + 1), (InStr(rsCommString, "*") - InStr(rsCommString, "$")) - 1)
              Else
                newUser.EmpID = "Not Specified"
              End If
            End If

            If IsDBNull(reader("createtimestamp").ToString) = True Then
              newUser.ctimestamp = ""
            Else
              newUser.ctimestamp = reader("createtimestamp").ToString
            End If

            If IsDBNull(reader("user_status").ToString) = True Then
              newUser.ustatus = ""
            Else
              newUser.ustatus = reader("user_status").ToString
            End If

            If IsDBNull(reader("user_suspend_date").ToString) = True Then
              newUser.susdt = ""
            Else
              newUser.susdt = reader("user_suspend_date").ToString
            End If
            lstUsers.Add(newUser)
          Else
            reader.NextResult()
          End If
        End If
      Loop
      con.Close()
    Catch ex As Exception
      MsgBox("User List data pull has failed:  " & ex.Message, vbExclamation, "ERROR")
      Exit Sub
    End Try

    arPtr = lstUsers.Count
    lstUsers.Sort()

    If rblSort.SelectedValue = 0 Then
      ddUser.Items.Clear()
      ddUser.Items.Add("")
      For arPtr = 0 To lstUsers.Count - 1
        Select Case lstUsers.Item(arPtr).ownerName
          Case "MWDW", "NEDW", "SDW", "WDW", "HQ", "RSL", "SPCL", "DEV"
            If InStr(lstUsers.Item(arPtr).comment, "~") > 0 And InStr(lstUsers.Item(arPtr).comment, "Admin Id") = 0 Then
              ddUser.Items.Add(lstUsers.Item(arPtr).username _
              & " : " & Mid$(lstUsers.Item(arPtr).comment, 1, (InStr(lstUsers.Item(arPtr).comment, "~") - 1)) _
              & " ~ " & Mid$(lstUsers.Item(arPtr).comment, (InStr(lstUsers.Item(arPtr).comment, "$") + 1), (InStr(lstUsers.Item(arPtr).comment, "*") - InStr(lstUsers.Item(arPtr).comment, "$")) - 1))
            End If
        End Select
      Next arPtr
    Else()
      ddUser.Items.Clear()
      ddUser.Items.Add("")
      For arPtr = 0 To lstUsers.Count - 1
        Select Case lstUsers.Item(arPtr).ownerName
          Case "MWDW", "NEDW", "SDW", "WDW", "HQ", "RSL", "SPCL", "DEV"
            If InStr(lstUsers.Item(arPtr).comment, "~") > 0 And InStr(lstUsers.Item(arPtr).comment, "Admin Id") = 0 Then
              ddUser.Items.Add(Mid$(lstUsers.Item(arPtr).comment, 1, (InStr(lstUsers.Item(arPtr).comment, "~") - 1)) _
              & " : " & lstUsers.Item(arPtr).username _
              & " ~ " & Mid$(lstUsers.Item(arPtr).comment, (InStr(lstUsers.Item(arPtr).comment, "$") + 1), (InStr(lstUsers.Item(arPtr).comment, "*") - InStr(lstUsers.Item(arPtr).comment, "$")) - 1))
            End If
        End Select
      Next arPtr
    End If

  End Sub

  Public Class userInfo
    Public username As String
    Public comment As String
    Public ownerName As String
    Public EmpID As String
    Public ctimestamp As String
    Public ustatus As String
    Public susdt As String
  End Class
#End Region

Open in new window

0
Comment
Question by:EDW_Gideon
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39945818
One option would be to use LINQ:
This link describes the sort method and provides an example.
http://msdn.microsoft.com/en-us/library/b0zbh7b6(v=vs.110).aspx
0
 

Author Comment

by:EDW_Gideon
ID: 39946063
I found that earlier but I don't think it's the same method I'm using in my code.  I first add my list items to a "newUser" list and add newUser to the lstUsers.  I got this process from a previously posted question here on EE...  Perhaps I'm using the method wrong?

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28299823.html

Sorry if this is a simple process perhaps I'm just not grasping the MSDN example.
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 39946228
You are building your list of users from a database query. Could you just sort the data in your query, or do you need to sort the list of users somewhere other than the code you provided?
0
 

Author Comment

by:EDW_Gideon
ID: 39946254
I initially thought of that but as the value was only a portion of one of the values I thought it may be easier to sort it after the fact.  I found some SQL that will allow me to pull this sub-string out as a field which will allow me to drive the sort from the query.  Thanks for your assistance.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 48
ASP.NET MVC 2 31
in Dot net,lastest version of MVC 3 24
Calculate number of nights between two dates 5 25
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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