Datatable select time in smalldatetime column
-
30-06-2021 - |
Question
I have a datatable
with 4 columns and one of them is smalldatetime
type. When I retrieve datatable from SQL base, I would like to query further through datatable
, but only by a specific time. Is this possible?
I am tryin with something like this:
DataRow[] rows = dataset.Tables[0].Select("resStart = 'some date '" + myTime+"'");
and specifically I am trying to navigate through dates and times in datagrid so finally it dhould retreive one row by query like this
DataRow[] rows = dataset.Tables[0].Select("resStart = '" + myDate +" " + myTime+"'");
Solution
If you want to query your data within your sourceCode DataTable class supports method SELECT which allows you to filter your table according to a given condition. Check out MSDN for a detailed explanation.
string filterExpression = "YOUR_SMALLDATE_COLUMN > #1/1/00#";
DataRow[] rows = YOUR_DATATABLE_VAR.Select(filterExpression);
// eg. print out results - or rebind data etc.
for(int i = 0; i < rows.Length; i ++)
{
Debug.WriteLine(rows[i]["ANY_COLUMN_NAME"]);
}
Of course, you may also change your select statement to the database and append a whereClause.
OTHER TIPS
Is this possible?
Yes of course. For example by using Linq-To-DataSet:
var thisYearsRows = table
.AsEnumerable()
.Where(r => r.Field<DateTime>("SmalldateTimeField").Year == DateTime.Now.Year);
You need to add using System.Linq;
and a reference to System.Data.DataSetExtensions.dll
.