DataView.RowFilter an ISO8601 - c#

I have a DataTable (instance named: TimeTable) whose DefaultView (instance named: TimeTableView) I am trying to use to filter based on a date. Column clock_in contains an ISO8601 formatted string. I would like to select all the rows in this DataTable/DefaultView between 2009-10-08T08:22:02Z and 2009-10-08T20:22:02Z.
What would I have to filter on this criteria? I tried:
TimeTableView = TimeTable.DefaultView;
TimeTableView.RowFilter = "clock_in >= #2009-10-08T08:22:02Z# and #2009-10-08T20:22:02Z#";
This is not working for me. Am I operating on the wrong object or is my filter syntax wrong?

In the end I solved it myself.
To get a specific date:
TimeTableView = TimeTable.DefaultView;
TimeTableView.RowFilter = String.Format("CONVERT(clock_in, System.DateTime) = #{0}#", dayToFilter.ToShortDateString());
To get a range of dates (where A and B are DateTime objects):
TimeTableView = TimeTable.DefaultView;
TimeTableView.RowFilter = String.Format("CONVERT(clock_in, System.DateTime) >= #{0}# AND CONVERT(clock_in, System.DateTime) <= #{1}#", A.ToShortDateString(), B.ToShortDateString());

Related

How to filter records on a string date column using entity framework

I have a SQL server database table that has a DueDate column. Data type of this column is nvarchar(50) and thus it saves dates values as string into this column.
I am using entity framework to get records from this table. My requirement is to get only those records whose DueDate is elapsed i.e records where DueDate <= TodaysDate. But as Linq to Entities does not support Covert.ToString() or Covert.ToDateTime() methods therefore I cannot compare the DueDate values with today's date value.
Kindly help me in fixing this problem.
Thanks in advance.
Try to parse your string to a date type, just like below:
I store the current date in a string (suppose it's like you read the date as a string from the database) and then I parse it to a datetime object.
string StringDate = DateTime.Now.ToString();
DateTime Date = DateTime.Parse(StringDate);
Try this.
DateTime startDate = DateTime.Now;
string dateToCompare = startDate.ToString("dd/MM/yyyy");
var result = from Table in context.Table where Table.DueDate.CompareTo(dateToCompare) <= 0
select Table.ID;
List<int> resultBrands = result.ToList(); // it should be select just Table but I will show the results to be clear.
Here is results, (I use a test db)
Hope helps,

Failed to get records when matching c# DateTime value against Date value in sql

I have a date column in a database table.
I'm using a LINQ expression that search records using today's date against this DOB column.
DateTime searchDate = DateTime.Now;
Expression<Func<Contact, bool>> cntExpression = p => p.DOB.HasValue && p.DOB == searchDate
Now, When I try to execute this expression then no record is returned. The reason is that I am passing 'DateTime' value in LINQ expression and the DOB column in the database table is of 'Date' type. So it's trying to match a datetime value against a date only value.
I already tried to use:
DateTime searchDate = DateTime.Now;
but it didn't solved the problem.
Please help me in fixing this issue.
Thanks in advance.
You need to take just the date part of the searchDate (at the end or anywhere before)
DateTime searchDate = DateTime.Now;
Expression<Func<Contact, bool>> cntExpression = p => p.DOB.HasValue && p.DOB == searchDate.Date
You say that the field in the database is of type DATE which doesn't contain any time information. You're trying to search against DateTime.Now which will contain the time. Try searching against DateTime.Today instead and removing the time portion of any other dates being searched by using searchDate.Date

simple.data orm, how to search by date?

I am using simple.data for my project. I have a column createdDate. this is a datetime column. if i passed in a date string "05/06/2006". I need to get all records created in that day. how can i do that using simple.data?
following code is not working. i only need to compare the date, not the time. hwo can i modify it to get it work.
var list = _db.DocumentLog.All();
if (!string.IsNullOrWhiteSpace(searchDate))
{
var dt = DateTime.ParseExact(searchDate, "MM/dd/yyyy", null);
list = list.Where(_db.DocumentLog.CreatedDate == dt);
}
Replace your search with:
list.Where(_db.DocumentLog.CreatedDate >= dt
&& _db.DocumentLog.CreatedDate < dt.AddDays(1));
That will give you everything created on or after midnight of the given date, but before the next day - i.e. one full day.

Filtering DateTime Column of DataTable Causes FormatException

I have a DataTable dt bound to a DataGridView. I have been filtering the data in this grid at run-time using the standard query syntax. However, for a known DateTime column in this DataTable the query
DataRow[] rowArray = dt.Select("DOB >= #01/01/97# AND DOB <= #31/01/97#");
But this is throwing a FormatException with the message:
String was not recognized as a valid DateTime.
I have tried changing the above to
DataRow[] rowArray = dt.Select("DOB >= #01/01/1997# AND DOB <= #31/01/1997#");
But this gave the same error. From MSDN it seems I am filtering correctly. I can confirm that the DataTable column 'DOB' is indeed a DateTime type.
What is wrong with the filter statement?
Thanks for your time.
The only thing I can think about is the Locale. This seems you use GB Locale. Try this.
CultureInfo myCultureInfo = new CultureInfo("en-gb");
dt.Locale = myCultureInfo;
DataRow[] rowArray = dt.Select("DOB >= #01/01/1997# AND DOB <= #31/01/1997#");
Just pass two DateTime variables into your Select statement like this: (based on your comment regarding inputting the date in text boxes, I also added some data validation)
DateTime startDate;
DateTime endDate;
var isValidStartDate = DateTime.TryParse(txtStartDate.Text, out startDate);
var isValidEndDate = DateTime.TryParse(txtEndDate.Text, out endDate);
if (isValidStartDate && isValidEndDate)
{
var rowArray = dt.Select(
string.Format("DOB >= #{0}# AND DOB <= #{1}#", startDate, endDate));
// do something with rowArray
}
else
{
// uh-oh...
}

datetime conversion with specific format

i want to filter the data from a datatable using dataview row filter Datatable having the column name month having like this "01-04-2012 00:00:00". I want to get the get the data using this format..
This is my partial code
dv1 = dtable.DefaultView;
DateTime start = new DateTime(DateTime.Now.Year, 4, 1,0,0,0);
dv1.RowFilter =Convert.ToString(start); //"Month = '04-01-2011 00:00:00'";
it returns the following error : Syntax error: Missing operand after '00' operator. I can't able to fix this error please help me to do this ...
You need to enclose the datetime value into #. You also have to say what column you want to compare with this value.
For example:
dv1.RowFilter = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"Date = #{0}#", start);
(where Date is the actual name of your datetime-column)
DataView.RowFilter Property
Using the InvariantCulture Property
Looking at the code, you are missing the name of the column you would want to filter on.
For example:
dv1.RowFilter = string.Format("PurchaseDate = '{0}'", Convert.ToString(start));
The assumption is that PurchaseDate is the date inside the view, the data is to be filtered using.

Categories