Converted C# To VB.NET , But Vb.net code Not working ,Below converted code is pasted .i got the error Linq part

StackOverflow https://stackoverflow.com/questions/22421772

  •  15-06-2023
  •  | 
  •  

Question

Dim dt As New DataTable("MyTable")
        dt.Columns.Add(New DataColumn("Name"))
        dt.Columns.Add(New DataColumn("Place"))
        dt.Columns.Add(New DataColumn("date", Type.[GetType]("System.String")))

create the data table

Dim dr As DataRow = dt.NewRow()
        dr("Name") = "500"
        dr("Place") = "Chennai"
        dr("date") = "10-May-2014"
        dt.Rows.Add(dr)
        Dim dr1 As DataRow = dt.NewRow()
        dr1("Name") = "600"
        dr1("Place") = "Chennai"
        dr1("date") = "11-May-2014"
        dt.Rows.Add(dr1)

added the row

        Dim dr2 As DataRow = dt.NewRow()
        dr2("Name") = "200"
        dr2("Place") = "Bangalore"
        dr2("date") = "12-Aug-2014"
        dt.Rows.Add(dr2)
        Dim dr3 As DataRow = dt.NewRow()
        dr3("Name") = "40"
        dr3("Place") = "Chennai"
        dr3("date") = "14-May-2014"
        dt.Rows.Add(dr3)
        Dim dr5 As DataRow = dt.NewRow()
        dr5("Name") = "9000"
        dr5("Place") = "Bangalore"
        dr5("date") = "15-Aug-2014"
        dt.Rows.Add(dr5)

added the row to datatable

Dim dr4 As DataRow = dt.NewRow()
        dr4("Name") = "9000"
        dr4("Place") = "Bangalore"
        dr4("date") = "1-Aug-2014"
        dt.Rows.Add(dr4)

This below part get error

Dim grouped = From groupbyUD In From userdefinedtable In dt.AsEnumerable()Group userdefinedtable By New With { _
            Key .placeCol = userdefinedtable("Place") _
        }New With { _
            Key .ValueUD = groupbyUD.Key, _
            Key .ColumnValuesUD = groupbyUD _
        }

Dim place As String = ""
        Dim [date] As String = ""
        Dim tempTable As New DataTable()
        Dim slectedFieldsTable As New DataTable()
        Dim newRow As DataRow
        Dim list As New List(Of Object)()

create the data table

slectedFieldsTable = New DataTable()
        slectedFieldsTable.Columns.Add("place")
        slectedFieldsTable.Columns.Add("date")

Also below var get the error and grouped also get the error.

For Each keyUD As var In grouped
            Console.WriteLine(keyUD.ValueUD.placeCol)
            place = DirectCast(keyUD.ValueUD.placeCol, String)
            Dim lst As New List(Of DateTime)()
            For Each columnValue As var In keyUD.ColumnValuesUD
                lst.Add(Convert.ToDateTime(columnValue("date")))
            Next
            Console.WriteLine(lst.Min())
            [date] = DirectCast(Convert.ToString(lst.Min()), String)
            slectedFieldsTable.Rows.Add(place, [date])
        Next

bind the value data set to list

        For Each drin As DataRow In slectedFieldsTable.Rows
            list.Add(drin)
        Next
        tempTable.Columns.Add("place", GetType(String))
        tempTable.Columns.Add("date", GetType(String))
        For Each drlst As DataRow In list
            newRow = tempTable.NewRow()
            newRow("place") = drlst.ItemArray(0).ToString()
            newRow("date") = drlst.ItemArray(1).ToString()
            tempTable.Rows.Add(newRow)
            tempTable.AcceptChanges()
        Next
        Console.ReadLine()
    End Sub
End Class

End Namespace

Was it helpful?

Solution

I think following code solve your problem. Replace code with yours.

Dim grouped = From userdefinedtable In dt.AsEnumerable() _
              Group userdefinedtable By Key = userdefinedtable("Place") Into Group _
              Select ValueUD = Key, ColumnValuesUD = Group

Replace as Var with as Object

For Each keyUD As Object In grouped
    Console.WriteLine(keyUD.ValueUD.placeCol)
    place = DirectCast(keyUD.ValueUD.placeCol, String)
    Dim lst As New List(Of DateTime)()
    For Each columnValue As Object In keyUD.ColumnValuesUD
        lst.Add(Convert.ToDateTime(columnValue("date")))
    Next
    Console.WriteLine(lst.Min())
    [date] = DirectCast(Convert.ToString(lst.Min()), String)
    slectedFieldsTable.Rows.Add(place, [date])   
Next
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top