Solved

LINQ + Dictionary Collection

Posted on 2011-09-15
16
355 Views
Last Modified: 2012-05-12
I have a dictionary collection which holds a set of connection objects,

Is there a way in LINQ to just retrieve connection of a certain type?

Public Class ConnectionMgr

    Inherits System.Collections.DictionaryBase

    Private Shared _connectionMgr As ConnectionMgr

    Public Shared Function GetInstance() As ConnectionMgr
        If _connectionMgr Is Nothing Then
            _connectionMgr = New ConnectionMgr
        End If
        Return _connectionMgr
    End Function

    Protected Sub New()
        Load()
    End Sub
    Public Sub Add(ByVal connection As Connection)
        Dictionary.Add(connection.ConnID, connection)
    End Sub

    Public Function Item(ByVal Key As Object) As Connection
        Return CType(Dictionary.Item(Key), Connection)
    End Function

    Public Sub Remove(ByVal Key As Object)
        Dictionary.Remove(Key)
    End Sub

    Private Sub Load()

        Dim oIConnection As IConnection
        Dim dr As DataRow
        Dim ds As DataSet

        oIConnection = CType(Activator.GetObject(GetType(IConnection), AppConstants.REMOTEOBJECTS & "ConnectionDC.rem"), IConnection)
        ds = oIConnection.LoadProxy()
        oIConnection = Nothing

        For Each dr In ds.Tables(0).Rows
            Dim connection As New Connection(DirectCast(dr.Item("CONN_ID"), Integer))
            With connection
                .Loading = True
                .ConnName = DirectCast(dr.Item("CONN_Name"), String)
                .ConnProvider = DirectCast(dr.Item("CONN_Provider"), String)
                .ConnConnectionString = DirectCast(dr.Item("CONN_ConnectionString"), String)
                .ConnType = CType(dr.Item("CONN_Type"), ConnectionType)
                .Loading = False
            End With
            Me.Add(connection)
        Next
        ds = Nothing

    End Sub

    Public Sub Refresh()
        Dictionary.Clear()
        Load()
    End Sub

Open in new window

0
Comment
Question by:rocky050371
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36546892
What "type" do you want to retrieve?  There are better ways to approach this than DictionaryBase.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36546957
Do you mean something like this inside your loop?
For Each dr In ds.Tables(0).Rows
    Dim xl = From xo In dr.ItemArray Where TypeOf (xo) Is ConnectionType Select xo
Next

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36547014
or more precisely this?
For Each dr In ds.Tables(0).Rows
    Dim connection As New Connection(DirectCast(dr.Item("CONN_ID"), Integer))
    With connection
        .Loading = True
        .ConnName = DirectCast(dr.Item("CONN_Name"), String)
        .ConnProvider = DirectCast(dr.Item("CONN_Provider"), String)
        .ConnConnectionString = DirectCast(dr.Item("CONN_ConnectionString"), String)
        .ConnType = (From xo In dr.ItemArray Where TypeOf (xo) Is ConnectionType Select xo).FirstOrDefault
        .Loading = False
    End With
    Me.Add(connection)
Next

Open in new window

0
 

Author Comment

by:rocky050371
ID: 36547756
The dictionary base collection is already there, I just need a way not to filter it


0
 

Author Comment

by:rocky050371
ID: 36547760
The actual filtering needs to happen somewhere else in the program not during the load
0
 

Author Comment

by:rocky050371
ID: 36547774
As a side note what would you have used for the connection manager instead of a dictionary class
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 36548029
Use a List(Of Connection) as your collection instead of the dictionary
You can then select by type:
    Public Function ConnectionsOfType(Type As ConnectionType) As List(Of Connection)
        Return cons.Where(Function(c) c.ConnType = Type).ToList
    End Function
0
 

Author Comment

by:rocky050371
ID: 36548042
The trouble is it is now fairly embedded, there must be a way of using link with a custom dictionary class
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 27

Expert Comment

by:MikeToole
ID: 36548308
Query the collection of Connections in the Dictionary, I think this will do it:

...
    Return Dictionary.Values.OfType(Of Connection).Where(Function(c) c.ConnType = Type).ToList
...
0
 

Author Comment

by:rocky050371
ID: 36548352
Sorry Mike based on my connectionmgr lass how would that work, from my calling form I do this


       _connectionMgr = ConnectionMgr.GetInstance

            'Filter list

            For Each entry In _connectionMgr
                connection = CType(entry.Value, Connection)
                If connection.ConnType = ConnectionType Then
                    lvi = New UltraListViewItem(connection.ToString)
                    lvi.Tag = connection.ConnID
                    lvi = ulvConnecitonList.Items.Add(connection.ConnID.ToString, connection.ToString)
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 36548451
This should filter the collection, then your for each would loop through FilteredList
dim FilteredList = _connectionMgr.Values.OfType(Of Connection).Where(Function(c) c.ConnType = ConnectionType).ToList

In VS 2010 an alternative to the For Each would be this:
        FilteredList .ForEach(
            Sub(connection)
                    lvi = New UltraListViewItem(connection.ToString)
                    lvi.Tag = connection.ConnID
                    lvi = ulvConnecitonList.Items.Add(connection.ConnID.ToString, connection.ToString)
            End Sub)
 
0
 

Author Comment

by:rocky050371
ID: 36548467
The dictionary class does not have a values option
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 36548571
The Dictionary class does have a Values property, but _connectionMgr doesn't. Try this:

    dim FilteredList = CType(_connectionMgr, IDictionary).Values.OfType(Of Connection).Where(Function(c) c.ConnType = ConnectionType).ToList
0
 

Author Comment

by:rocky050371
ID: 36548707
You a star mike, worked a treat. Do you need the function option or can you just use c.ConnType = ConnectionType
0
 
LVL 27

Accepted Solution

by:
MikeToole earned 500 total points
ID: 36548776
When using the method-based syntax in LINQ you need the the Function() definition - essentially the Where() method needs a Boolean parameter,  the Function supplies that.
The alternative is to use the LINQ query syntax (which is translated to the method syntax by the compiler) which might look a little cleaner.
Something like:

dim FilteredList = From c In CType(_connectionMgr, IDictionary).Values.OfType(Of Connection)
                          Where  c.ConnType = ConnectionType Select c

Then FilteredList.Tolist will return a List(of Connection)
0
 

Author Comment

by:rocky050371
ID: 36548901
Thanks again Mike
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

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A short film showing how OnPage and Connectwise integration works.
Send secure, cloud-based, encrypted alerts and maintain HIPAA compliant messaging. Integrates priority & secure messaging into one application. Ensures IT, emergency respondents and healthcare professionals that their critical messages are never mis…

911 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

21 Experts available now in Live!

Get 1:1 Help Now