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.
Related
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,
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
How can I check the date I pick from a DateTimePicker control exists in a SQL table?
I have a DateTimePicker on my ASP.NET page where I can select a date. I need to check if this selected date exists in a SQL table (in a table called blockdate). If the selected date does exist, I need to enter the corresponding values from the date and reason columns in the blockdate table into a DataGridView.
Can anybody tell me how I can do this?
This is the code I have so far to check if the selected date is a weekend.
DatePicker.SelectedDate.ToString("dd-MM-yyyy");
DateTime date = DatePicker.SelectedDate.Date;
DateTime weekend;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
weekend = date.AddDays(+2);
}
if (date.DayOfWeek == DayOfWeek.Sunday)
{
weekend = date.AddDays(+1);
}
else
{
weekend = date;
}
I think calling the database on every time when the DatePicker values change not going to be an ideal solution.
You can select all the dates from you sql table to a list in pageload.
var dateList = new List<DateTime>();
dateList = //all the dates from you sql table
Then when the DatePicker value changed, you can match the value against your list.
Hope this helps
Hello I posted a few days ago a similar question and I was told to use a dataset filter to filter my datagridview. I've had a go but I'm struggling to work out how to do it.
I'm trying to filter a deadline column in a datagridview by 2 datetimepickers - startDate and endDate.
datagridview is TaskTable2, datetimepicker1 is startSchedule, datetimepicker2 is endSchedule and deadline in datagridview is deadlineRow
TaskDataSet in the underlying datasource.
So far I have got the following code which is successfully making the rows invisible which are not between the selected start and end date.
private void scheduleButton_Click(object sender, EventArgs e)
{
DateTime startSchedule = startDate.Value.Date;
DateTime endSchedule = endDate.Value.Date;
if (startSchedule <= endSchedule)// runs foreach loop if startdate and enddate are valid
{
foreach (DataGridViewRow dr in TaskTable2.Rows)// loops through rows of datagridview
{
string deadline = dr.Cells["Deadline"].Value.ToString(); // gets deadline values
DateTime deadlineRow = Convert.ToDateTime(deadline); // converts deadline string to datetime and stores in deadlineRow variable
if (startSchedule <= deadlineRow && deadlineRow <= endSchedule) // filters deadlines that are => startDate and <= endDate
{
dr.Visible = true; // display filtered rows here.
}
else
{
dr.Visible = false; // hide rows that are not beteen start and end date.
TaskTable2.CurrentCell = null;
}
}
}
else
{
MessageBox.Show("Please ensure Start Date is set before End Date."); // ensures user selects an end date after the start date.
}
}
However, I have a few existing problems:
Using the visible property is the wrong approach, I need to filter the dataset using something like the following (not sure how to do it)
TaskDataSet.Filter = "startSchedule <= Deadline AND Deadline <= endSchedule";
I have a print button that is supposed to print the filtered
results. However, it is printing all data stored in the
datagridview, even if some rows are visible=false from pressing the
schedule button so this is why I need to filter the dataset and then
use that in the print event.
The datagridview is bound to an XML file so data can be removed from the datagridview for filtering and printing aslong as they remain in the XML file.
If someone could amend my code with a dataset filter rather than using the visible property it would be greatly appreciated.
Thanks!
You filter code could be:
DateTime startSchedule = startDate.Value.Date;
DateTime endSchedule = endDate.Value.Date;
TaskDataSet.Filter = "Deadline >='" + startSchedule + "' AND Deadline <= '" + endSchedule + "'";
As far as your second issue with the printing of the filtered results - found this link online in VB.NET but you could convert it to C#
Print DataSet or DataTable contents from VB.NET
You can also try the DataSet.Clone() and loop through the rows to remove the filtered items then print the remaining records.
DataGridView Printing by Selecting Columns/Rows
How can I print data from a DataGridView in C#?
suppose that your dataset is named 'ds' and the data is stored in the first table, you could do this:
ds.Tables[0].DefaultView.RowFilter =
string.Format("Deadline between ( {0}, {1})",startSchedule,endSchedule );
Note that I didn't test this.
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());