惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - 黃偉榮

Web Project的檔案共用小技巧 IoC的中繼器:CommonServiceLocator UTF8Encoding與BOM Temporary Post Used For Theme Detection (d4b0aefa-c88e-4957-bba7-b367d1bfa042 - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 寫CodedUI時如何尋找控制項的小技巧 Visual Studio 2010 Feature Packs 2之Silverlight自動化測試 Moles - Isolation framework for .NET(假.Net)介紹 [小技巧]Entity Framework強型別Include C#仿Oracle Decode,將ValueType對應成String - 黃偉榮 - 博客园 Visual Studio 單元測試的3種Initialize與Cleanup jQuery套件-檢查頁面的欄位是否有變更 用EventLogReader查詢特殊EventLog jQuery自製Plugin-Bind事件函式時檢查有沒有Bind過 ASP.NET MVC TempData使用心得 Visual Stuiod 自訂檔案比較合并工具 [小技巧]自動化測試時NLog的訊息輸出到測試結果中 小技巧:專案切換32與64位元組件 Unit Test小技巧 : DateTime的Stub 解決TFS Build Asp.Net Mvc開啟MvcBuildViews後無法載入組件問題
Linq小技巧:日期處理
黃偉榮 · 2010-09-14 · via 博客园 - 黃偉榮

Linq處理日期的小技巧,你知道嗎?Linq對DateTime的處理如:屬性、方法,是支援轉譯的,原來Linq的DateTime處理可以這樣寫。

測試工具:LinqPad 4.26

測試環境:

  • .Net Framework 4.0
  • Sql Server 2008 R2(因為手邊沒有2008以下的環境,沒試過在2008以下會怎麼樣)

DateTime的屬性如:Date、Year,方法如:AddDays、AddYears,都會轉成Sql,知道轉譯的方法,對操作Linq To Sql或Entity Framework會更方便上手,下面介紹幾個情況給大家參考,詳細內容請看MSDN。

1.Today或某天的處理

如果要找出今天修改的資料,你會怎麼下,像下面這樣嗎?

from p in Products
where p.ModifiedDate >= DateTime.Today && p.ModifiedDate < DateTime.Today.AddDays(1)
select p
-- 轉譯的Sql
SELECT *
FROM [Product]
WHERE ([ModifiedDate] >= '2010-09-13 00:00:00.000' ) AND ([ModifiedDate] < '2010-09-14 00:00:00.000')

其實還有更簡單的方式

from p in Products
where p.ModifiedDate.Date == DateTime.Today
select p
-- 轉譯的Sql
SELECT *
FROM [Product]
WHERE CONVERT(DATE, [ModifiedDate]) = '2010-09-13 00:00:00.000'

是不是簡單許多呢,當然也可以跟某一天比較,如:

from p in Products
where p.ModifiedDate.Date == new DateTime(2010,1,1)
select p

Tip:

DateTime.Date,是去掉時間只取日期,而DATE,是Sql Server 2008 的只有日期的資料型態。

2.找出某年月的資料

from p in Products
where p.ModifiedDate.Year == DateTime.Today.Year && p.ModifiedDate.Month == DateTime.Today.Month
select p
-- 轉譯的Sql
SELECT *
FROM [Product]
WHERE (DATEPART(Year, [ModifiedDate]) = 2010) AND (DATEPART(Month, [ModifiedDate]) = 9)

3.加半年就過期的資料

from p in Products
where p.ModifiedDate.AddMonths(6) >= DateTime.Today
select p
-- 轉譯的Sql
SELECT *
FROM [Product]
WHERE DATEADD(MONTH, 6, [PeriodDate]) >= '2010-09-14 00:00:00.000'

Note:

用AddHours,AddMinutes,AddSeconds,轉譯的T-Sql,非常的不易閱讀,不過還好平常沒事不用管轉譯結果,長怎麼樣。

如AddHours(6),會轉譯成

DATEADD(ms, (CONVERT(BigInt,6 * 3600000)) % 86400000, DATEADD(day, (CONVERT(BigInt,6 * 3600000)) / 86400000, [ModifiedDate]))

我個人覺得用Linq在很多情況下,比T-Sql直覺好用多了,用了二年的Linq,我想我的T-Sql一定退化了很多。

參考資料

  1. System.DateTime 方法 (LINQ to SQL)