Solved

Sorting a System.Collections.Generic List

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

Expert Comment

by:Shaun Kline
Comment Utility
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
Comment Utility
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 25

Accepted Solution

by:
Shaun Kline earned 500 total points
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

18 Experts available now in Live!

Get 1:1 Help Now