We help IT Professionals succeed at work.

Convert C# to VB.NET

I am using Visual Studio 2010 with the .Net 4.0 Framework.

return table.AsEnumerable().Select(r => columns.Select(c =>
     new { Column = c.ColumnName, Value = r[c] })
     .ToDictionary(i => i.Column, i => i.Value != DBNull.Value ? i.Value : null));

Open in new window


I am trying to convert the code above to something I can use in a web service written in vb.net.  I found this code at Telerik Blogs

Here is the code that I have already converted:

    <OperationContract()> _
    Public Function GetData(ByVal Company As String, ByVal Location As String, ByVal ObjectID As Integer) As IEnumerable(Of Dictionary(Of String, Object))
        Dim mgr As New ObjectManager(Company, Location, ObjectID)
        Dim table As DataTable = mgr.GetDataTable

        Dim columns As DataColumn = table.Columns.Cast(Of DataColumn)()

        ' Insert new line here

    End Function

Open in new window


NOTE:  I have already tried to plug this into a C# to VB.NET convertor it doesn't work.
Comment
Watch Question

i think you are dealing with compatibility issue it may take sometime before you will get a hold on some tool which conversion for version 4
x77

Commented:
I Do´nt understand  the "ToDictionary" From Sample,

I think  "ToList"   - List  Pair     ColumnName, Value

    Dim table As New DataTable
    table.Columns.AddRange(New DataColumn() { _
       New DataColumn("A", GetType(Integer)), _
       New DataColumn("b", GetType(Integer))})

    table.Rows.Add(1, 2)
    table.Rows.Add(3, 4)

    Dim X = (From r In table.AsEnumerable From c As DataColumn In table.Columns _
     Select New With {c.ColumnName, .V = r(c)})

    For Each v In X
        Debug.Print("{0,-5} : {1}", v.ColumnName, v.V)
    Next


A     : 1
b     : 2
A     : 3
b     : 4

Author

Commented:
Shahan,

It doesn't work.  I think I even tried this already.  It gives me:

Return table.AsEnumerable().[Select](Function(r) columns.[Select](Function(c) New With { _
      .Column = c.ColumnName, _
      .Value = r(c) _
}).ToDictionary(Function(i) i.Column, Function(i) If(i.Value <> DBNull.Value, i.Value, Nothing)))

The error message that I am getting states that it failed because no accessible Select can be called with these arguments.
x77

Commented:
   Dim X = (From r In table.AsEnumerable From c As DataColumn In table.Columns _
     Select New With {c.ColumnName, .V = r(c)}). _
     ToDictionary(Function(p) p.ColumnName, Function(p) If(p.V Is DBNull.Value, Nothing, p.V))

Note:  This throws exception when Table.Rows > 1
           Dictionary Key (ColumnName) is unique only if table has only one row.
Commented:
ToLookup Group values on each column

    Dim X = (From r In table.AsEnumerable From c As DataColumn In table.Columns _
     Select New With {c.ColumnName, .V = r(c)}). _
     ToLookup(Function(p) p.ColumnName, Function(p) If(p.V Is DBNull.Value, Nothing, p.V))

    For Each k In X
        Debug.Print(k.Key)
        For Each value In X(k.Key)
            Debug.Print("   " & value.ToString)
        Next
    Next

Author

Commented:
x77,

It wasn't quite the solution that I was looking for but it was enough for me to realize what I was looking at and rewrite it to work.  The completed working code is attached:
Public Function GetData(ByVal Company As String, ByVal Location As String, ByVal ObjectID As Integer) As IEnumerable(Of Dictionary(Of String, Object)) Implements IEKService.GetData
        Dim mgr As New Informediate.ExecuKeys.Data.ObjectManager(Company, Location, ObjectID)
        Dim table As DataTable = mgr.GetDataTable

        Dim columns = table.Columns.Cast(Of DataColumn)()

        Return table.AsEnumerable.Select(Function(r) columns.Select(Function(c) New With { _
              .Column = c.ColumnName, _
              .Value = r(c) _
        }).ToDictionary(Function(i) i.Column, Function(i) If(i.Value Is DBNull.Value, Nothing, i.Value)))
    End Function

Open in new window

Author

Commented:
Not quite complete but enough to get me there.  Thank you for the help.