GRChandrashekar
asked on
Order by and Select TOP in Datable
Hi, I have a datatable. I need to sort this by MODIFIED-DATE DESC and return top 1 row in output. How do I do this ?
or using linq:
datatable.Rows.OrderByDescending(n=>n["MODIFIED-DATE"]).FirstOrDefault();
sorry didn't see it's vb.net:
and linq:
dmi dv as DataView = datatable.DefaultView
dv.Sort = "MODIFIED-DATE desc"
dim drv as DataRowView = dv(0)
and linq:
datatable.Rows.OrderByDescending(function(n) n["MODIFIED-DATE"]).FirstOrDefault()
ASKER
How to return the output in data table ?
Public Shared Function Lastmodifiedrecord(dt As DataTable) As DataTable
Try
Dim datatable = DataTableCloneClass.ClonedDataTable(dt)
_dview = Nothing
_dview = datatable.DefaultView
_dview.Sort = "MODIFIEDDATE DESC"
Dim drv As DataRowView = _dview(0)
-----------?
Catch ex As Exception
ErrorHandlerClass.LogMessage(ex.Message + ex.StackTrace)
Throw
End Try
End Function
Public Shared Function Lastmodifiedrecord(dt As DataTable) As DataTable
Try
Dim datatable = DataTableCloneClass.ClonedDataTable(dt)
_dview = Nothing
_dview = datatable.DefaultView
_dview.Sort = "MODIFIEDDATE DESC"
dim tableOrder = DataTableCloneClass.ClonedDataTable(dt)
tableOrder.Add(_dview(0).Row)
return tableOrder
Catch ex As Exception
ErrorHandlerClass.LogMessage(ex.Message + ex.StackTrace)
Throw
End Try
End Function
ASKER
tableOrder.Add(_dview(0).R ow)
.add shows error.
.add shows error.
what error?
ASKER
.add syntax is wrong
change it to:
tableOrder.Rows.Add(_dview(0).Row.ItemArray)
ASKER
Public Shared Function Lastmodifiedrecord(dt As DataTable) As DataTable
Try
_dview = Nothing
_dview = DataTableCloneClass.ClonedDataTable(dt).DefaultView
[b] _dview.Sort = "MODIFIEDDATE DESC"[/b]
Dim drv As DataRowView = _dview(0)
Dim tableOrder = DataTableCloneClass.ClonedDataTable(dt)
tableOrder.Rows.Add(_dview(0).Row.ItemArray)
Return tableOrder
Catch ex As Exception
ErrorHandlerClass.LogMessage(ex.Message + ex.StackTrace)
Throw
End Try
End Function
Till the high lighted line it is fine. After that it is not sorted and returning all records
in line 6, do u get the top 1 row according to sorting criteria?
ASKER
No
line 6 is actually redundant, u can remove it from your code.
on line 8 i get the top 1 row after the sorting took place.
on line 8 i get the top 1 row after the sorting took place.
ASKER
removed line 6
what change to make in line 8
tableOrder.Rows.Add(_dview (0).Row.It emArray)
what change to make in line 8
tableOrder.Rows.Add(_dview
nothing really, _dview should be updated after setting the .Sort property.
can u make sure that ModifiedDate field is of type DateTime?
can u make sure that ModifiedDate field is of type DateTime?
have u managed to run this?
ASKER
Tried but didnt work though line 5 sorts correctly in DESC order
so if the sorting is working properly, then check _dview(0).Row which is the first row after the sorting took place, which means the top 1.
ASKER
When it comes to this Dim tableOrder = DataTableCloneClass.Cloned DataTable( dt)
sorting goes off
sorting goes off
ASKER
Tried with this but no luck again
_dview = Nothing
_dview = DataTableCloneClass.ClonedDataTable(dt).DefaultView
_dview.Sort = "MODIFIEDDATE DESC"
_dview.Table.AsEnumerable().Take(1)
Return _dview
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window