久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 LINQ 中將字符串轉換為日期時間值

Convert string to datetime value in LINQ(在 LINQ 中將字符串轉換為日期時間值)
本文介紹了在 LINQ 中將字符串轉換為日期時間值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設我有一個表,以 String 格式存儲日期時間 (yyyyMMdd) 列表.我如何提取它們并將它們轉換為日期時間格式 dd/MM/yyyy ?

Suppose I have a table storing a list of datetime (yyyyMMdd) in String format. How could I extract them and convert them into DateTime format dd/MM/yyyy ?

例如20120101 -> 01/01/2012

e.g. 20120101 -> 01/01/2012

我嘗試了以下方法:

var query = from tb in db.tb1 select new { dtNew = DateTime.ParseExact(tb.dt, "dd/MM/yyyy", null); };

但結果是ParseExact函數無法識別的錯誤.

But it turns out the error saying that the ParseExact function cannot be recgonized.

推薦答案

通過 AsEnumerable 在本地而不是在數據庫中進行解析可能是值得的:

It's probably worth just doing the parsing locally instead of in the database, via AsEnumerable:

var query = db.tb1.Select(tb => tb.dt)
                  .AsEnumerable() // Do the rest of the processing locally
                  .Select(x => DateTime.ParseExact(x, "yyyyMMdd",
                                                CultureInfo.InvariantCulture));

初始選擇是為了確保只獲取相關列,而不是整個實體(僅對于其中大部分將被丟棄).我也避免使用匿名類型,因為這里似乎沒有意義.

The initial select is to ensure that only the relevant column is fetched, rather than the whole entity (only for most of it to be discarded). I've also avoided using an anonymous type as there seems to be no point to it here.

順便說一下,請注意我是如何指定不變文化的 - 您幾乎肯定不想只想使用當前文化.我更改了用于解析的模式,因為聽起來您的 source 數據采用 yyyyMMdd 格式.

Note how I've specified the invariant culture by the way - you almost certainly don't want to just use the current culture. And I've changed the pattern used for parsing, as it sounds like your source data is in yyyyMMdd format.

當然,如果可能的話,您應該更改數據庫架構以將日期值存儲在基于日期的列中,而不是作為文本.

Of course, if at all possible you should change the database schema to store date values in a date-based column, rather than as text.

這篇關于在 LINQ 中將字符串轉換為日期時間值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 中文成人无字幕乱码精品 | 成人午夜影院 | 精品av| 日韩α片| 久久久久一区二区三区 | 国产精品视频免费看 | 久久久免费 | 综合色站导航 | 鲁大师一区影视 | 成人精品国产一区二区4080 | 欧美一级免费看 | 久久91精品久久久久久9鸭 | 欧美精品1区 | 久久精品久久久久久 | 人人做人人澡人人爽欧美 | 亚洲一区欧美一区 | 91av在线影院 | 日韩在线精品视频 | 日韩中文字幕 | 亚洲一区二区三区视频在线 | av手机免费在线观看 | 亚洲男人的天堂网站 | 中文字幕日韩欧美 | 精品国产99| 精品国产一区一区二区三亚瑟 | 欧美一区二区三区日韩 | 黄色大片网 | 成人免费精品视频 | 亚洲视频 欧美视频 | 成人在线观看免费视频 | 在线欧美亚洲 | 伊人网综合在线 | 国产成人精品a视频一区www | 中文字幕av中文字幕 | 国产高清在线精品一区二区三区 | 人干人人 | 超碰伊人久久 | 日本三级网站在线 | 欧美精品久久 | 日韩免 | 九九在线视频 |