none
Converting Nested Looping Into LINQ RRS feed

  • Question

  • Hi there, 

    I'm still new to LINQ.

    I'm creating a function to get maximum value cross columns for each rows in datagridview.

    example:

    col1, col2, col3
    90, 70, 80
    1, 3, 2 
    10, 9, 8
    30, 40, 50

    Result of this function is: 90, 3, 10, 50.

    And this is the function codes

    Private Function GetMaxCol() As String
            Dim val As New List(Of String)
            Dim max As Integer
            For Each row As DataGridViewRow In dgv.Rows
                max = 0
                For Each col As DataGridViewCell In row.Cells
    
                    If max < CInt(col.Value) Then max = CInt(col.Value)
                Next
                val.Add(max.ToString)
            Next
    
    
            Return String.Join(",", val.ToArray)
    End Function

    Is there anyway to convert this block of codes into LINQ style?

    Thanks.


    ===================== Best Regards, Che Siung

    Monday, December 18, 2017 3:34 AM

Answers

  • Try this kind of LINQ too:

    Private Function GetMaxCol() As String
    
       Return String.Join(",", dgv.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).Select(Function(r) r.Cells.Cast(Of DataGridViewCell).Where(Function(c) IsNumeric(c.Value)).Max(Function(c) CInt(c.Value))))
    
    End Function
    

    • Marked as answer by Che Siung Monday, December 18, 2017 7:24 AM
    Monday, December 18, 2017 6:43 AM

All replies

  • Hi Che Siung,

    Firstly please check if datagridview bind data, if yes, you can use the following code:

    Private Sub MySql_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Dim dt As New DataTable 'dt = fun(1) Dim dt As New DataTable dt.Columns.Add("col", GetType(Integer)) dt.Columns.Add("co2", GetType(Integer)) dt.Columns.Add("co3", GetType(Integer)) dt.Rows.Add(90, 70, 80) dt.Rows.Add(1, 3, 2) dt.Rows.Add(10, 9, 8) dt.Rows.Add(30, 40, 50) DataGridView1.DataSource = dt End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim dt As DataTable = TryCast(DataGridView1.DataSource, DataTable) Dim result = From r In dt.AsEnumerable() Select r.ItemArray.Max()

    End Sub

    If datagridview is not binding, you could need to convert datagridview to datatable:

     Private Sub MySql_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
            DataGridView1.ColumnCount = 3
            DataGridView1.Columns(0).Name = "col1"
            DataGridView1.Columns(1).Name = "col2"
            DataGridView1.Columns(2).Name = "col3"
    
            Dim row As String() = New String() {"90", "70", "80"}
            DataGridView1.Rows.Add(row)
            row = New String() {"1", "3", "2"}
            DataGridView1.Rows.Add(row)
            row = New String() {"10", "9", "8"}
            DataGridView1.Rows.Add(row)
            row = New String() {"30", "40", "50"}
            DataGridView1.Rows.Add(row)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            'Dim dt As DataTable = TryCast(DataGridView1.DataSource, DataTable)
            Dim dt As DataTable = dgridViewTods(DataGridView1)
            Dim result = From r In dt.AsEnumerable()
                         Select r.ItemArray.Max()
    
            For Each r As Integer In result
                Console.WriteLine("The value is {0}", r)
            Next
        End Sub
    
        Private Function dgridViewTods(ByVal dgv As DataGridView) As DataTable
            Dim dt As New DataTable
            Try
                ' Add Columns
                Dim col As DataColumn
                For Each dgvCol As DataGridViewColumn In dgv.Columns
                    col = New DataColumn(dgvCol.Name)
                    dt.Columns.Add(col)
                Next
    
                'Add Rows from the datagridview
                Dim row As DataRow
                Dim colcount As Integer = dgv.Columns.Count - 1
                For i As Integer = 0 To dgv.Rows.Count - 1
                    row = dt.Rows.Add
                    For Each column As DataGridViewColumn In dgv.Columns
                        row.Item(column.Index) = dgv.Rows.Item(i).Cells(column.Index).Value
                    Next
                Next
    
                Return dt
    
            Catch ex As Exception
    
                MsgBox("CRITICAL ERROR : Exception caught while converting dataGridView to DataSet (dgvtods).. " & Chr(10) & ex.Message)
                Return Nothing
            End Try
        End Function

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 18, 2017 5:54 AM
    Moderator
  • Try this kind of LINQ too:

    Private Function GetMaxCol() As String
    
       Return String.Join(",", dgv.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).Select(Function(r) r.Cells.Cast(Of DataGridViewCell).Where(Function(c) IsNumeric(c.Value)).Max(Function(c) CInt(c.Value))))
    
    End Function
    

    • Marked as answer by Che Siung Monday, December 18, 2017 7:24 AM
    Monday, December 18, 2017 6:43 AM
  • Dear Cherry,

    Thanks for your reply.

    My datagridview is unbind to any data source.

    And I want to "convert" my looping for with linq command.

    But anyway, thank you for your help.


    ===================== Best Regards, Che Siung

    Monday, December 18, 2017 7:21 AM
  • Dear Viorel, 

    Thank you for your reply, and it works.



    ===================== Best Regards, Che Siung

    Monday, December 18, 2017 7:26 AM