Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GETDATE() in LINQ

Tags:

sql

linq

What is the alternate for GETDATE() in LINQ?

I got some workaround from the following post:

  • How do I use SQL's GETDATE() and DATEADD() in a Linq to SQL expression?

Is there any better approach? In which, there is no need to an ExecuteQuery or a separate function call?

like image 380
Bisileesh Avatar asked Oct 17 '12 06:10

Bisileesh


1 Answers

As far as I can see with linq to sql the post you refer to is the way to go. I don't see how anything could be better. It's a pretty elegant solution.

With Entity Framework there is a possibility to use SqlFunctions.CurrentTimestamp. Besides that, even when you use DateTime.Now in linq to entities (not linq to sql) it is translated to GetDate:

context.Companies.Select (c => DateTime.Now);

translates:

SELECT 
GetDate() AS [C1]
FROM [dbo].[Company] AS [Extent1]

It's a different story if you only want to get the database date. In that case I don't see how anything could beat executing a query SELECT GetDate().

like image 190
Gert Arnold Avatar answered Oct 04 '22 00:10

Gert Arnold