J. Raoul FEZE
J. Raoul FEZE

Reputation: 47

How to remove the Time part of a Date in a Linq Query

I tried to remove the Time part of a Date from a Linq query to no avail. I used the DbFunctions.TruncateTime but it didn't work. Here is the my code

    var Cycle1 = 
        ((from sbm in context.SBMs 
         where sbm.CrewSite.SiteID == site.SiteID 
         select new Cycle { Date = DbFunctions.TruncateTime(sbm.CrewSite.Crew.TodayDate) }
        ).OrderBy(x => x.Date).Skip(1).FirstOrDefault()).Equals(null)
        ? (DateTime?)null 
        : ((from sbm in context.SBMs 
          where sbm.CrewSite.SiteID == site.SiteID 
          select new Cycle { Date = DbFunctions.TruncateTime(sbm.CrewSite.Crew.TodayDate) }
          ).OrderBy(x => x.Date).Skip(1).FirstOrDefault()).Date

Can someone help me see what i did wrong ?

Upvotes: 0

Views: 311

Answers (2)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14228

As i can see, your value has been removed time part.

All you need to do is format the Date in ListView

Your case looks like this

<asp:Label Text='<%# Eval("Cycle1", "{0:yyyy-MM-dd}") %>' runat="server" ID="Cycle1Label" /></td>

Upvotes: 2

thatsalok
thatsalok

Reputation: 133

Though you already get the answer, however I see, you calling db query two times.. this has significant impact, possible for you to change above code like this

var cycleObj = ((from sbm in context.SBMs 
          where sbm.CrewSite.SiteID == site.SiteID 
          select new Cycle { Date = DbFunctions.TruncateTime(sbm.CrewSite.Crew.TodayDate) }
          ).OrderBy(x => x.Date).Skip(1).FirstOrDefault()); 

var Cycle1 = cycleObj == null? (Date?)cycleObj.Date : (Date?) null;

Upvotes: 0

Related Questions