tags:

views:

681

answers:

2

I am trying to use entity framework and get the date from the SQL server. The old way of doing this in LINQ is below but i'm needing something for the entity framework:

Partial Public Class ActivityActionsDataContext
<[Function](Name:="GetDate", IsComposable:=True)> _
Public Function GetSystemDate() As DateTime
    Dim mi As MethodInfo = TryCast(MethodBase.GetCurrentMethod(), MethodInfo)
    Return DirectCast(Me.ExecuteMethodCall(Me, mi, New Object() {}).ReturnValue, DateTime)
End Function

End Class

A: 

Have you tried using DateTime.Now or DateTime.UtcNow? I'd like to think that would be converted into GetSystemDate() by the query translator.

Jon Skeet
That give the time and works but if the application is running in a different timezone then it will be off this is way i need the time of the server and not where the application is running. Thanks anyways
OneSmartGuy
It's not clear to me whether you want the UTC time or not, but have you tried both? You might want to try DateTimeOffset as well, which is more definite on these matters.
Jon Skeet
var qry = (from col in db.ActivityActions select DateTime.Now); Debug.WriteLine((qry as ObjectQuery).ToTraceString());Seams to produce what i need. Is there way to do this without using from a table?
OneSmartGuy
A: 

Search at google "efquerysamples" entity framework samples and download those good examples then look canonical function CurrentDateTime(), it worked with:

DateTime fecha = ServicioContexto.Contexto.CreateQuery("CurrentDateTime()").Execute(MergeOption.NoTracking).First();

I'd like to understand more about canonical functions. Please explain me. Thank you

related questions