Solved

LINQ + Dictionary Collection

Posted on 2011-09-15
16
353 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
Comment Utility
What "type" do you want to retrieve?  There are better ways to approach this than DictionaryBase.
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
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
Comment Utility
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
Comment Utility
The dictionary base collection is already there, I just need a way not to filter it


0
 

Author Comment

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

Author Comment

by:rocky050371
Comment Utility
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
Comment Utility
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
Comment Utility
The trouble is it is now fairly embedded, there must be a way of using link with a custom dictionary class
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The dictionary class does not have a values option
0
 
LVL 27

Expert Comment

by:MikeToole
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again Mike
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now