Compare DateTime Objects

9 posts, 0 answers
  1. Oboro
    Oboro avatar
    10 posts
    Member since:
    Jul 2012

    Posted 09 Oct 2009 Link to this post

    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!!!


  2. Oboro
    Oboro avatar
    10 posts
    Member since:
    Jul 2012

    Posted 09 Oct 2009 Link to this post

    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.


  3. DevCraft banner
  4. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 14 Oct 2009 Link to this post

    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.
  5. HISEagle
    HISEagle avatar
    3 posts
    Member since:
    Sep 2011

    Posted 31 Jul 2012 Link to this post

    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?
  6. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 03 Aug 2012 Link to this post

    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.
  7. Mlamuli
    Mlamuli avatar
    2 posts
    Member since:
    Jul 2015

    Posted 10 Nov 2015 Link to this post

    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();

  8. Ady
    Admin
    Ady avatar
    589 posts

    Posted 12 Nov 2015 Link to this post

    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.
  9. Mlamuli
    Mlamuli avatar
    2 posts
    Member since:
    Jul 2015

    Posted 12 Nov 2015 in reply to Ady Link to this post

    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

  10. Ady
    Admin
    Ady avatar
    589 posts

    Posted 17 Nov 2015 Link to this post

    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.
Back to Top
DevCraft banner