Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ + Dictionary Collection

Posted on 2011-09-15
16
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

 

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
 
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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

704 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