Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I want use between clause using Linq in asp.net c#.

What I have tried:

if(txtFromdate.Text != "" && txtTodate.Text != "")
            {
                DateTime fromDate = Convert.ToDateTime(txtFromdate.Text);
                DateTime toDate = Convert.ToDateTime(txtTodate.Text);

                _candidates = from c in _candidates
                              where c.Registration >= fromDate && c.Registration <= toDate
                              select c;

            }


but the above code is not returning any record.

Can any one plz help me.


Thanks in advance.
Posted
Updated 30-Jul-17 19:48pm
Comments
Thomas Daniels 30-Jul-17 10:13am    
What's the value of fromDate, toDate, and _candidates (before the query)?
abdul subhan mohammed 30-Jul-17 10:18am    
fromDate: 05/05/2017 & ToDate: 30/07/2017
Thomas Daniels 30-Jul-17 10:19am    
Okay. Could you share a few items of _candidates?
Afzaal Ahmad Zeeshan 30-Jul-17 10:21am    
Maybe there are no candidates in that search range. If so, this is totally fine.

If not, then can you provide samples — as ProgramFOX has asked?
Kornfeld Eliyahu Peter 30-Jul-17 10:22am    
The code looks all right, so if it returns nothing it probably has nothing to return...
What in _candidates without where clause? What in Registration column? Remember, that NULL value is not comparable...

1 solution

public class candidates
   {
       public string Name;
       public string Dept;
       public string ID;
       public DateTime Registration;
   }
   class Program
   {
       static void Main(string[] args)
       {

           DateTime fromDate = new DateTime(2017, 7, 31, 9, 30, 0);
           DateTime toDate = new DateTime(2017, 7, 31, 11, 00, 0);

           List<candidates> _candidate = new List<candidates>();

           for (int i = 0; i <= 5; i++)
           {
               candidates c = new candidates();

               c.Dept = "ABC";
               c.ID = i.ToString();
               c.Name = "Candidate " + i.ToString();
               c.Registration = new DateTime(2017, 7, 31, 14-i, 00, 0);

               _candidate.Add(c);
           }

          var _icandidates = from c in _candidate where c.Registration >= fromDate
                             && c.Registration <= toDate select c;

       }
 
Share this answer
 
v3
Comments
sreeyush sudhakaran 31-Jul-17 1:51am    
This works fine in testing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900