Click here to Skip to main content
15,885,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i want to display information from database using the date given. i want to specify the information is between the two dates stated in the view

but i get the error in the query saying that
"Operator '>=' cannot be applied to operands of type 'TimeSpan?' and 'DateTime'"

datestart and dateend is from the input form

What I have tried:

var query2 = (from c in db.Information
                            where c.Time_start >= DateStart && c.Time_start <= DateEnd
                            select new DetailBundle
                            {
                                Time_start = c.Time_Start
                            }).ToList();
            ViewBag.datepick = query2;
Posted
Updated 13-Jul-20 23:25pm

You can't compare a Timespan and a DateTime - that like trying to work out the larger of an egg and the theory of relativity.

So check the types of Time_start (which is probably a TimeSpan?) and DateStart (which I suspect is a DateTime) - and either convert the Timespan to a DateTime or vice versa.

Which you chose is going to depend on what you are trying to do, and we don't know that - unless DateStart and DateEnd are the same day then you have a problem as a TimeSpan doesn;t have a "fixed start point" and you can't really say it's between two dates at all.
Think about it: is "09:00" between 1st Feb 1990 and 3rd Aug 2021? It appears once every day, but that isn't really helpful ...
 
Share this answer
 
Comments
dlnzki 14-Jul-20 13:32pm    
DateTime is taken from an input form. i already change the data type and it works. thanks!
OriginalGriff 14-Jul-20 14:27pm    
You're welcome!
C#
where c.Time_start >= DateStart

These two operands are different types as clearly noted in the error message. You need to change one of them so they are the same, both DateTime or TimeSpan types.
 
Share this answer
 

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