Add minutes from one column to datetime column in linq query - c#

I have a column in my database named Created which is a datetime and a column named Minutes which is a varchar(5) and will contains values such as 0, 60, 120, etc.
How can I write a LINQ statment that only returns those records:
where Created + Minutes has gone passed the current time?
Thank you!
--- Update: code so far ---
var results = from emails in MyDAL.Context.Emails
emails.Created.Value.AddMinutes(double.Parse(emails.Minutes)) > DateTime.Now
select emails;

For others looking for the answer, here is how I ended up making it work....
System.Data.Objects.EntityFunctions.AddMinutes(emails.Created, emails.Minutes) < DateTime.Now

Well you could try:
DateTime now = DateTime.UtcNow;
var query = from record in db.Records
where record.Create.AddMinutes(int.Parse(record.Minutes)) > now
select record;
Whether that will work or not depends on the LINQ provider...

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,

EF6 Check to see if a Record exists for a specified Date

I am generating a statistics table for an already populated table . The EUR is the source table and Stats is the Destination Table.
I am wanting one row in the destination table for each day there are records in the source table.
I have this bit of code
DateTime dt = date.Date; // Source Date
destinationRow =
dme.Statistics.FirstOrDefault(d => d.DateString.Date == dt); // Does a row for this day exist?
so I iterate through each row in the source table and get the Date I am then trying to see if the source table has a row for that Date - but EF Complains that I can not use Date within the Query.
So how can I check the destination table for a row with a specific Date without querying the table twice. Once to get a DateTime, convert it and check and a further time to update or create the row?
You can use the TruncateTime method like this:
var result = dme.Statistics
.FirstOrDefault(d =>
DbFunctions.TruncateTime(d.DateString) == dt);

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.

Obtain all records received within last six months

New to Linq to Entity, trying to get all records received within the last six months. I have spent the last several hours trying to get this to work. Any assistance would be greatly appreciated. When I call the 'limit' variable it is being assigned the date 01/01/0001. Any assistance would be appreciated. It works if I comment out the 'where' clause; however, I need it to be sorted by only the last six months.
Thanks in advance.
JobSeekersEntities context = new JobSeekersEntities();
var limit = DateTime.Today.AddMonths(-6);
var query = from c in context.Applications
where c.received > limit
orderby c.received descending
select new { c.firstName, c.middleName, c.lastName, c.street, c.city, c.state, c.zip, c.position };
var results = query.Take(25).ToList();
applicationDataGrid.DataContext = results;
If you stop the debugger at the line "var limit = " you will get that value. You need to press F10 to step over that code then look at value, it will be correct. Have to let that line run so that limit gets assigned. Var in this case is DateTime, which is a value type so it has a default value. I could see this being misleading.

Categories