There are times when you want to perform query on only date part of the column that has type DateTime in database. Entity framework does not support DateTime.Date property but provides a helper class EntityFunctions. EntityFunctions has a method TruncateTime that can be called in Linq2Entities query and truncates the time part from datetime. Here is how to use it
DateTime dtEnd=DateTime.Now.Date;
using (AccountEntities dal = new AccountEntities ())DateTime dtStart=DateTime.Now.AddDays(-20).Date;
{
DateTime dtEnd=DateTime.Now.Date;
transactions = (from t in dal.Transactions
where
EntityFunctions.TruncateTime(t.TransactionDate.Value)
>= dtStart
&&
EntityFunctions.TruncateTime(t.TransactionDate.Value)
<= dtEnd
orderby t.TransactionDate descending
select t).ToList<Transaction>();
}
Comments
Post a Comment
Share your wisdom