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.
Asked
Active
Viewed 3,268 times
3
-
1Look at this post:http://stackoverflow.com/questions/200617/how-do-i-use-sqls-getdate-and-dateadd-in-a-linq-to-sql-expression – Amir Sep 14 '11 at 06:23
-
I was looking at that code before, but I didn't know how to express it in VB.Net, but thank you :) – Reigo Hein Sep 14 '11 at 07:02
-
C# > VB.NET > C# converter: http://www.developerfusion.com/tools/convert/csharp-to-vb/ – Chad Levy Sep 14 '11 at 08:41
-
@CjCoax The post you suggest is about Linq to SQL, not Entity Framework – surfen Nov 13 '11 at 23:34
-
1This is more related: http://stackoverflow.com/questions/683496/getdate-using-entity-framework – surfen Nov 14 '11 at 00:03
1 Answers
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 -
1This 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