how to write linq query for comparing date between start & end date

Answered how to write linq query for comparing date between start & end date

  • Thursday, August 04, 2011 5:44 AM
     
     

    hi Team,

    autally my reqiurement is that i can selected start and end dates from DatePicker and i have to compare them such way i.e,

    all records records comes in between them are show in datagrid.

    my all records are presented in obserable collection list(my list name is ladlelist)

    pls send me as possible .

    thanks in advance.........................

All Replies

  • Thursday, August 04, 2011 5:46 AM
     
     
  • Thursday, August 04, 2011 6:04 AM
     
     

    my linq query is that is not working:-

    var s = from p in ladlelist
            where p.lf_start <= dpkFromDate.SelectedDate && p.lf_end >= dpkToDate.SelectedDate
            select p;
     datagriname.itemsource=p;

    but in Datepicker date format is not supported

  • Thursday, August 04, 2011 6:13 AM
     
     

    but in Datepicker date format is not supported

    which format you have for your datepicker. it support date format you can check the format  of the datepicker

  • Thursday, August 04, 2011 6:26 AM
     
     

    no autually i mean to say that in my database list dates  r in like that format "15-06-2011 14:00:45"

    but i m not able to compare dates only(like as 6/15/2011) which is come from Datepicker

    pls help me to resolve this problem

  • Thursday, August 04, 2011 6:28 AM
     
     
  • Thursday, August 04, 2011 6:29 AM
     
     

    hi mohan,

    what is data type of If_start.??

    if it's datatime you can use Convert.ToDateTime Method

    for more about date conversion see following post.


  • Thursday, August 04, 2011 6:38 AM
     
     

    lf_start is date format

    i m already trying this Convert.ToDateTime  but this is not working where dates come from datePicker 

  • Thursday, August 04, 2011 6:46 AM
     
     

    how this going to work?

    becs i dont wnt to comapre date to returns an integer that indicates whether the first instance is earlier than other but i wanted to those records which comes in between these dates and show into datagrid

  • Thursday, August 04, 2011 6:52 AM
     
     

    becs i dont wnt to comapre date to returns an integer that indicates whether the first instance is earlier than other but i wanted to those records which comes in between these dates and show into datagrid

     

    as you are checking the between two dates so you need to check for bigger from one date and lesser from second date

    Hope that will help

  • Thursday, August 04, 2011 6:54 AM
     
     
  • Thursday, August 04, 2011 7:13 AM
     
     

    i don't understand  pls send me some sample code

     

  • Thursday, August 04, 2011 7:21 AM
     
     

    i don't understand  pls send me some sample code

     

     

    try this

      var s = from p in customerList
                        where p.StartDate<= dtpDatePicker.SelectedDate.Value && p.EndDate >= dtpDatePicker.SelectedDate.Value
                        select p;

    Hope that will help let me know if you have any question

  • Friday, August 05, 2011 12:09 AM
     
     

    hi friends,

    i m using this code but this is not working

    var s = from p in ladlelist where p.lf_start <= dpkFromDate.SelectedDate.Value && p.lf_end >= dpkToDate.SelectedDate.Value

    select p;

    then i also using this code but this is also not working

    var s = from p in ladlelist where p.lf_start <= Convert.toDateTime(dpkFromDate.SelectedDate.Value) && p.lf_end >= Convert.toDateTime(dpkToDate.SelectedDate.Value) select p;

    ples help to resolve this problem.

  • Monday, August 08, 2011 2:10 AM
     
     Answered

    Hi,

    I think you should try this:

    var s = from p in ladlelist where p.lf_start <=(DateTime) (dpkFromDate.SelectedDate.Value??DateTime.MinValue) && p.lf_end >= (DateTime)(dpkToDate.SelectedDate.Value ?? DateTime.MinValue)
    select p;

    Because, the SelectedDate is a nullable datetime, so we can't compare between a DateTime and DateTime? directly.