Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to truncate time part from date in linq query?

Hi I am trying to write linq query to get some details from Sql table. I have created column and storing date and time both. while returning i want to ommit time part. May I know is this possible?

  List<returnObject> obj = new List<returnObject>();
obj = (from c in objectDB.NCT_Project
  join user in objectDB.NCT_UserRegistration on c.adminUserId equals user.User_Id
  where c.adminUserId == userId
                       select new returnObject
                       {
                           id = c.project_Id,
                           key = c.key,
                           created = c.createdDate //currently returns datetime
                       }).ToList(); 

Any help would be appreciated. Thank you.

Upvotes: 6

Views: 26520

Answers (4)

MelOS
MelOS

Reputation: 653

If you can get date comparison out of the LINQ and leave the rest there, you can use this syntax:

sqlite.Query<Entity>("date comparison").Where("other queries")

The predicate I used in the Query() function had to return only todays orders and looked something like this:

select * from Order where date(orderDate/ 10000000 - 62135596800, 'unixepoch') = date('now')

Upvotes: -1

YuvShap
YuvShap

Reputation: 3835

Use DbFunctions.TruncateTime method:

created = DbFunctions.TruncateTime(c.createdDate)

According to the docs:

When used as part of a LINQ to Entities query, this method invokes the canonical TruncateTime EDM function to return the given date with the time portion cleared.

Upvotes: 18

Drew Aguirre
Drew Aguirre

Reputation: 393

you can try this one.

 created = c.createdDate.ToString("HH:mm")
 created = c.createdDate.ToString("H:mm") 
 created = c.createdDate.ToString("hh:mm tt") 
 created = c.createdDate.ToString("h:mm tt")

also see this question : How to get only time from date-time C#

Upvotes: 1

Divisadero
Divisadero

Reputation: 913

All you need to do is call 'Date' property on createdDate.

select new returnObject
                       {
                           id = c.project_Id,
                           key = c.key,
                           created = c.createdDate.Date
                       }).ToList(); 

Upvotes: 2

Related Questions