3

I am using LINQ with Entity Framework, and I need to get the SQL server time. Can anyone tell me how can I create a method so it retrieves the current server time? Thank you.

Reigo Hein
  • 741
  • 9
  • 21

1 Answers1

6

Just execute a ExecuteStoreQuery command.

Dim dateTimeVal As DateTime = context.ExecuteStoreQuery(Of DateTime)("select getdate()").First()
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • It tells me that DateTime is a type and cannot be used as an expression and ExecuteStoreQuery says "Overload resolution failed because no accessible 'ExecuteStoreQuery' accepts this number of arguments – Reigo Hein Sep 14 '11 at 06:40
  • 1
    @Reigo my answer was in C#. Converted it to vb.net. See the updated answer. – Eranga Sep 14 '11 at 06:50
  • Hmm, it says "Expression expected, about the "Of" in . Sorry, I am familiar with C# aswell, but not so much with Vb.net. – Reigo Hein Sep 14 '11 at 06:56
  • Oh, I can just use context.ExecuteStoreQuery(Of DateTime)(.....). Well thats a bit awkward syntax for me, but thank you very much, it solved the problem! :) – Reigo Hein Sep 14 '11 at 07:00
  • 1
    @Reigo Replace `` with `(Of DateTime)`. Sorry i am not familiar with vb.net. – Eranga Sep 14 '11 at 07:01
  • 1
    This post helped me to find a solution for C# .NET 3.5: try `return this.CreateQuery("CurrentDateTime()").Execute(MergeOption.NoTracking).First();` – surfen Nov 14 '11 at 00:04