Solved

Sorting a System.Collections.Generic List

Posted on 2014-03-21
4
334 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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