2

I'm trying to select rows from a datatable that a have a datetime column that contains a certain dd/mm In my windows forms application using C#, how can this be done? I know the expressions are similar to SQL.

DataType of column is datetime, for example: 09/03/2017 13:26:40

so far i have tried the below but it just returns an exception:

string selectExpression = "colDate LIKE '%09/03%'";
DataRow[] rows = dataTable.Select(selectExpression);
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69

1 Answers1

1

You are getting the exception because you are apply string filter to DateTime. You can use it of the type if colDate is string then I tested your code and its working.

To filter the DataTable column with DateTime type you can use linQ query like given as under.

var rows = dataTable.AsEnumerable().Where(r=>r.Field<DateTime>("colDate").Day == 9
           && r.Field<DateTime>("colDate").Month == 3);
Adil
  • 146,340
  • 25
  • 209
  • 204