Linq query to extract values of first row from datatable

How can I extract the column values of very first row in datatable using linq query?

Ex : there 2 row item
Xyz
Abc

I need to extract the first row value ie.,xyz

How can we achieve this using linq query

you can try this it will pick all the row of xyz

dt.AsEnumerable().First()("Xyz").ToString()

@yashashwini2322

Hi @yashashwini2322

You can try this as well :

DataRow firstRow = dataTable.AsEnumerable().FirstOrDefault();

Hi @yashashwini2322 ,
you can try:
getting the first row - returned as a list (of DataRows)
YourDataTableVar.AsEnumerable.Take(1).toList

Getting the first 1 column values as an array
YourDataTableVar.AsEnumerable.Take(1).Select(Function (x) x(YourColNameOrIndex).toString.Trim).toArray

regards,

Hi @yashashwini2322

Dim firstRowValue As String = (From row In yourDataTable.AsEnumerable()
                               Select row.Field(Of String)("ColumnName")
                              ).FirstOrDefault()

If Not String.IsNullOrEmpty(firstRowValue) 
Then
    ' Use 'firstRowValue' as needed in your workflow
    LogMessage("First Row Value: " & firstRowValue)
Else
    LogMessage("DataTable is empty or the specified column doesn't exist.")
End If

Hope it helps!!

Hi @yashashwini2322

Try this:
outputValue = dt.AsEnumerable().First()("ColumnName").ToString()

Hello @yashashwini2322

Assuming you have a DataTable named “dt” with at least one row and one column
Dim firstValue As String = dt.AsEnumerable().Select(Function(row) row.Field(Of String)(“ColumnName”)).FirstOrDefault()

Check if the DataTable is not empty before accessing the value
If Not String.IsNullOrEmpty(firstValue) Then
Use “firstValue” in your workflow
LogMessage("First Value: " & firstValue)
Else
LogMessage(“DataTable is empty or the column does not exist.”)
End If

Thanks & Cheers!!!

@yashashwini2322

assign activity
str_output=datatable.rows(0)(“columnname”).tostring

gives you the first row value
no need of linq query

cheers

@yashashwini2322

DT1.AsEnumerable.Select(function(x) x.ItemArray).ToArray(0)

HopeItHelps!!

you can try this:

input_dt.AsEnumerable.Take(1).Select(Function (x) x(“xyz”).toString.Trim)

@yashashwini2322

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.