This is a migrated thread and some comments may be shown as answers.
Compare DateTime Objects
8 Answers 84 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Oboro
Top achievements
Rank 1
Oboro asked on 09 Oct 2009, 05:58 AM
Please, how do I compare datetime object with openaccess linq. Am trying to run the following query:

budgetAllocated.Where(a => Convert.ToDateTime(a.DateAllocated).Month == DateTime.Today.Month); 

But I keep getting the Error "System.Convert:ToDateTime(object) not supported on server side.

Is there another way I can get this done. HELP!!!


8 Answers, 1 is accepted

Sort by
0
Oboro
Top achievements
Rank 1
answered on 09 Oct 2009, 06:11 AM
Finally found a way around it. Instead of using Convert.ToDateTime, I used cast:

var budgetAllocated = from a in _scope.Extent<BudgetDetail>() 
                              where(((DateTime)a.DateAllocated).Month == DateTime.Today.Month && 
                                    ((DateTime)a.DateAllocated).Year == DateTime.Today.Year) 
                              select a; 

This worked.


0
Alexander
Telerik team
answered on 14 Oct 2009, 01:20 PM
Hello Jite,

You have found the right solution. Calling outer methods in Linq queries is not yet supported.

Best wishes,
Alexander
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
HISEagle
Top achievements
Rank 2
answered on 31 Jul 2012, 09:43 PM
What if the field you need to convert to datetime in your where clause is a string field rather than a date field in the database? Trying to use (DateTime)tc.fieldname results in an error that strings cannot be implicitly converted to DateTime. Is there a way around this?
0
Alexander
Telerik team
answered on 03 Aug 2012, 09:06 AM
Hi,

As a workaround in this case you could use the SQL<T> extension method that OpenAccess provides.
Here is an example how to achieve that for MSSQL using the convert function:
var result = (from p in context.MyClasses
         where "{0} < convert(datetime, {1})".SQL<bool>(p.DateTimeField, p.StringField)
         select p).ToList();

This should work for other backends as well, just the SQL in the string literal would be different.
Hope that helps.

All the best,
Alexander
the Telerik team
OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
0
Mlamuli
Top achievements
Rank 1
answered on 10 Nov 2015, 06:20 AM

hi

I also have a problem  I want my app to give me the expiry date  in two months advance  from the current date but I want that notification to stay there until the last date  which will be an Expiry date .my query does work but the notification doesn't last till the last day  of Expiry date please help  

 

here is my query

 

var query = (from b in con.tblNewMachines
where b.Machine_Exp_Date >= DateTime.Now.AddMonths(2) &&
b.Machine_Exp_Date < DateTime.Now.AddMonths(3)
select new
{
b.Machine_Tag,
b.Machine_Type,
b.Machine_Exp_Date
});
GridExpMachines.DataSource = query;
GridExpMachines.DataBind();

0
Ady
Telerik team
answered on 12 Nov 2015, 04:59 PM
Hi Mlamuli,

 I am afraid I haven't completely understood your problem. Does your query run without any error but doesn't return the desired result? Can you ensure that the datetime values in the database are in the same timezone as specified in the condition (and not stored as UTC)? 
Can you provide some more details about the problem.

Regards,
Ady
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
Mlamuli
Top achievements
Rank 1
answered on 13 Nov 2015, 05:38 AM

Hi Ady

ok lets me put it simple this time I want to show machines to be expire in two months but I want those machines to stay in my RadGrid untill the last day  or until they Expire complete then  Automatically they will be moved to another grid of machines that are Expired . hope you will understand me now but don't worry about those that are already expired  that one is already sorted

0
Ady
Telerik team
answered on 17 Nov 2015, 10:39 AM
Hello Mlamuli,

 From what I understand you should be querying for all objects with an expiry date within the next 2 months.
I would write the query as follows - 

01.var query = (from b in con.tblNewMachines
02.where b.Machine_Exp_Date >= DateTime.Now &&
03.b.Machine_Exp_Date <= DateTime.Now.AddMonths(2)
04.select new
05.{
06.b.Machine_Tag,
07.b.Machine_Type,
08.b.Machine_Exp_Date
09.});

This would give you all the objects that expire within the next 2 months and would continue to return the object till you reach the expiry date.

Hope this is what you want.
Do get back in case you need further assistance.


Regards,
Ady
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
LINQ (LINQ specific questions)
Asked by
Oboro
Top achievements
Rank 1
Answers by
Oboro
Top achievements
Rank 1
Alexander
Telerik team
HISEagle
Top achievements
Rank 2
Mlamuli
Top achievements
Rank 1
Ady
Telerik team
Share this question
or