Solved

LINQ + Dictionary Collection

Posted on 2011-09-15
16
357 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
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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 Textbox input validation 4 61
Adding items to a C# list incrementally 5 58
vb.net convert long time to mm:ss 23 20
Nested forach loop to linq 3 24
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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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