Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.

Answered Where Date = Date Only

  • Tuesday, May 22, 2012 9:37 PM
     
      Has Code

    Hello, how would I write a query in LINQ that will look at the date only and exclude the time for the following query?  Thanks in advance!

    Return (From incident In db.incidents _
    Where incident.rep_datetime = (ReportDate) _
                            Order By incident.inc_no _


    Brett The Jet

All Replies

  • Wednesday, May 23, 2012 6:01 AM
    Moderator
     
     Answered Has Code

    Hi brmcdani44,

    Seems like you could achieve this requirement by ToShortDateString() method and please refer to the following code snippet:

                var date = from d in db.Employees
                           where d.EmployeeID==1
                           select d.HireDate.Value.ToShortDateString();
    
    
                foreach (var item in date)
                {
                    Console.WriteLine(item);
                }

    Hope this could help you.

    Best Regards,


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us

    • Marked As Answer by brmcdani44 Thursday, May 24, 2012 9:15 PM
    •  
  • Wednesday, May 23, 2012 6:12 AM
     
      Has Code

    Hi,

    Your can use Date property in datetime object like the below

    Return (From incident In db.incidents _
    Where incident.rep_datetime.Date.CompareTo(ReportDate)==0 _
                            Order By incident.inc_no _

    This is the best practice to compare only dates instead of converting into string and comparing.

    Hope it will help you...


    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.
    MCP ID:Microsoft Transcript.

  • Wednesday, May 23, 2012 10:09 AM
     
      Has Code
    return (from incident In db.incidents
    where incident.rep_datetime.Date = ReportDate.Date
                            Order By incident.inc_no)

    This will be translated to

    DECLARE @p0 DateTime = '[Here Will Go The date from parameter] 00:00:00.000'
    
    SELECT [t0].[CaseID], [t0].[ParentID], [t0].[CFICardID], [t0].[UserID], [t0].[Status], [t0].[ExpDate], [t0].[isFlagged] AS [IsFlagged], [t0].[unid] AS [Unid], [t0].[CRTime], [t0].[OldID]
    FROM [Cases] AS [t0]
    WHERE CONVERT(DATE, [t0].[CRTime]) = @p0


    Please mark as reply if helped.
    Also visit my blog http://msguy.net/