locked
DateDiff in linq!!!!!!!!!!!! RRS feed

  • Question

  • hi Experts,

    plese help me in dong the Datediff using Linq ..

    condition:

    datediff btw startdate and todaysdate = 10 in linq

    example:

     

     var results = from p in dt.AsEnumerable()
                            
                              where (p.Field<DateTime>("Created").Day - Datetime.Now.ToString()) == 10
                              
                              select p;

     

    error iam getting as:

    - Can't be applied operands of int and String.

    Monday, July 12, 2010 8:17 AM

Answers

  • I tried this on my data and it works correctly. Try to display result of Subtract function:

     

    var a = from item in dataContext.SomeTable
        let b = DateTime.Now.Subtract(item.SomeDateColumn.Date).Days
        select new { D1 = item.SomeDateColumn.Date, D2 = DateTime.Now.Date, Diff = b };
    
    

     

    Also I'm wondering why you must use too many ! in subject. Probably you think, you will get answer quickly ? ;-)

     

    • Marked as answer by Bin-ze Zhao Thursday, July 15, 2010 7:50 AM
    Monday, July 12, 2010 11:43 AM
  • On 7/12/2010 4:17 AM, haroon2k8 wrote:
    > hi Experts,
    >
    > plese help me in dong the Datediff using Linq ..
    >
    > condition:
    >
    > datediff btw startdate and todaysdate = 10 in linq
    >
    > example:
    >
    > var results = from p in dt.AsEnumerable()
    >
    > where (p.Field<DateTime>("Created").Day - Datetime.Now.ToString()) == 10
    >
    > select p;
    >
    > error iam getting as:
    >
    > - Can't be applied operands of int and String.
    >
     
    What did you use DateTime.Now.Tostring()? You can't subtract a string
    value from a numeric value.
     
     
    What are you trying to do get the number of days between two dates? If
    that's the case, then simply do this.
     
    play with this first to know that it works.....
     
    int date1 = Convert.ToInt32(DateTime.Now.AddDays(5).ToString("yyyyMMdd"));
     
    int date2 = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
     
    var numberofdays = date1 - date2;
     
     
     
    where Convert.ToInt32(p.Field<DateTime>("Created").ToString("yyyyMMdd")
    -
    Convert.ToInt32(Datetime.Now.ToString("yyyyMMdd) == 10
     
     
    Well, that's one way of doing it. I don't remember going across
    leap-years or not in the calculation.
     
    You can also use ddyyyymm too but you'll have to deal with decimal points.
     
    This is from the old school mainframe days.
     
     
    But I think you're going to have to reverse the order DateTime.Now -
    Created to get the number of days between the two dates.
     
     
    • Marked as answer by Bin-ze Zhao Thursday, July 15, 2010 7:51 AM
    Monday, July 12, 2010 11:46 AM

All replies

  • Hi,

    try

    where (p.Field<DateTime>("Created").Date.Subtract(DateTime.Now.Date).Days == 10)

     

    Regards,

    Vinil;

    Monday, July 12, 2010 8:45 AM
  • Thanks for the reply..

    i tried but iam unable to fetch the reult set..can you please see code / syntax wrong here.

     

     DataTable dt = GetDataTableFromWSS("Listname");

                var results = from p in dt.AsEnumerable()
                              where (p.Field<DateTime>("Created").Date.Subtract(DateTime.Now.Date).Days == 10)
                              select new {
                                  Status = p.Field<string>("Status"),
                                  Title = p.Field<string>("Title")
                                   };
                GridView1.DataSource = results;

                GridView1.DataBind();

     

    Monday, July 12, 2010 9:06 AM
  • Oops,

    Try it other way..:)

    where (DateTime.Now.Date.Subtract(p.Field<DateTime>("Created").Date).Days == 100)

    Regards,

    Vinil;

    Monday, July 12, 2010 9:06 AM
  • Oops,

    Try it other way..:)

    where (DateTime.Now.Date.Subtract(p.Field<DateTime>("Created").Date).Days == 100)

    Regards,

    Vinil;


    sorry..iam not getting any  result ..plese help me
    Monday, July 12, 2010 9:10 AM
  • I tried this on my data and it works correctly. Try to display result of Subtract function:

     

    var a = from item in dataContext.SomeTable
        let b = DateTime.Now.Subtract(item.SomeDateColumn.Date).Days
        select new { D1 = item.SomeDateColumn.Date, D2 = DateTime.Now.Date, Diff = b };
    
    

     

    Also I'm wondering why you must use too many ! in subject. Probably you think, you will get answer quickly ? ;-)

     

    • Marked as answer by Bin-ze Zhao Thursday, July 15, 2010 7:50 AM
    Monday, July 12, 2010 11:43 AM
  • On 7/12/2010 4:17 AM, haroon2k8 wrote:
    > hi Experts,
    >
    > plese help me in dong the Datediff using Linq ..
    >
    > condition:
    >
    > datediff btw startdate and todaysdate = 10 in linq
    >
    > example:
    >
    > var results = from p in dt.AsEnumerable()
    >
    > where (p.Field<DateTime>("Created").Day - Datetime.Now.ToString()) == 10
    >
    > select p;
    >
    > error iam getting as:
    >
    > - Can't be applied operands of int and String.
    >
     
    What did you use DateTime.Now.Tostring()? You can't subtract a string
    value from a numeric value.
     
     
    What are you trying to do get the number of days between two dates? If
    that's the case, then simply do this.
     
    play with this first to know that it works.....
     
    int date1 = Convert.ToInt32(DateTime.Now.AddDays(5).ToString("yyyyMMdd"));
     
    int date2 = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
     
    var numberofdays = date1 - date2;
     
     
     
    where Convert.ToInt32(p.Field<DateTime>("Created").ToString("yyyyMMdd")
    -
    Convert.ToInt32(Datetime.Now.ToString("yyyyMMdd) == 10
     
     
    Well, that's one way of doing it. I don't remember going across
    leap-years or not in the calculation.
     
    You can also use ddyyyymm too but you'll have to deal with decimal points.
     
    This is from the old school mainframe days.
     
     
    But I think you're going to have to reverse the order DateTime.Now -
    Created to get the number of days between the two dates.
     
     
    • Marked as answer by Bin-ze Zhao Thursday, July 15, 2010 7:51 AM
    Monday, July 12, 2010 11:46 AM
  • Just use

    Namespace:  System.Data.Linq.SqlClient

    SqlMethods

    var test = from r in mytable   Select.SqlMethods.DateDiff(EndDate, StartDate)


    Friday, August 16, 2013 7:57 PM