Solved

LINQ + Dictionary Collection

Posted on 2011-09-15
16
358 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
Industry Leaders: 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!

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Filesystem watcher not working 5 56
YouTube API get the Video Tags - vb.net 7 51
Nested forach loop to linq 3 42
FTP recursive download files 5 27
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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