Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I use a date and time picker to find transaction( From date & To date)

I am having the issue that not all the transaction are displaying because of the time portion of the dateandtime.
But what i need is simply to display all transactions of the dates between datefrom and Enddate regardless of the time.

And i need it in object format not string format.

this is what i use :

CSS
DateTime FromDate =  DateTime.Now;

DateTime EndDate = DateTime.Now;



C#
var Transaction = (from c in context.JobCards
                   where (c.TransDateRequired > FromDate) && (c.TransDateAdded < EndDate)
                   select c).ToList();



I Also tried to truncate the time

C#
var Transaction = (from c in context.JobCards
                   where (EntityFunctions.TruncateTime(c.TransDateRequired) > EntityFunctions.TruncateTime(FromDate)) && (EntityFunctions.TruncateTime(c.TransDateRequired) < EntityFunctions.TruncateTime(EndDate))
                   select c);


With the truncate time i get following error

LINQ to Entities does not recognize the method 'System.Nullable`1[System.DateTime] TruncateTime(System.Nullable`1[System.DateTime])' method, and this method cannot be translated into a store expression<br />

thanks in advance for any help.
Posted
Updated 25-Aug-14 19:16pm
v2

You could build your query by using lambda expression like in the next example and should work:
C#
var Transaction = context.JobCards.Where(c => EntityFunctions.TruncateTime(c.TransDateRequired) > EntityFunctions.TruncateTime(FromDate)) && (EntityFunctions.TruncateTime(c.TransDateRequired) < EntityFunctions.TruncateTime(EndDate)));
 
Share this answer
 
Try this:
C#
//Since date columns accepts null in your db table
//Make it nullable
DateTime ?FromDate =  DateTime.Now;
 
DateTime ?EndDate = DateTime.Now;

var Transaction = (from c in context.JobCards 
                                    where (c.TransDateRequired >= FromDate 
                                        && 
                                    c.TransDateAdded <= EndDate)
                                    select c).ToList();

--Amy
 
Share this answer
 
v3
Comments
Dr Drastic 25-Aug-14 3:30am    
Hi Amy,

Thanks for the help.

I had the >= in the transaction var and the database does allow null.
the database field is dateandtime.

the problem is if a transaction is lets say at 09:00 the previous day and you try to retreive a transaction at 10:00 it doesnt import the transaction because of the time that is before the current time.

and what i am trying to do is ignore the time and use only date from a dateandtime table in the database.
What ive done is formatted selection dateandtime so that time is always 12:00 AM,

Now on date from it will use selected date and enddate it will use date +1 day up to 12:00 AM.

so selecetion will only be date between selections.

Thanks for help and suggestions
 
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