Reputation: 125
DataTable dt = db.Execute("select statement...");
I have this datatable which contains some data with a status column and a date column.
from that data I filter to get only the rows that have a specific status
DataRow[] potentialRows = dt.Select("status = 1");
Now here's my question:
from this DataRow[] of potential rows I want to further filter it to get only one DataRow which contains the closest date to today
(e.g.: if I have two rows: one with a date of yesterday and one with a date of a week ago, I want the DataRow that has the date of yesterday)
I tried the following:
DataRow dr = potentialRows.Max(x => x["date"]) as DataRow;
but this doesn't seem to work so I must be missing something.
thank you for any comment or solution.
Upvotes: 0
Views: 959
Reputation: 125
This solved my problem:
dr = potentialRows.OfType<DataRow>().OrderBy(r => r["date"]).LastOrDefault<DataRow>();
ordering the array by the date field and grabbing the last element which holds the most current date.
Upvotes: 1