簡體   English   中英

C# Linq 如果日期以字符串格式存儲在數據庫中,則 2 個日期之間的日期

[英]C# Linq Where Date Between 2 Dates if date stored in database in string format

我想在兩個日期之間使用 c# linq 獲取數據,但存儲在數據庫中的日期是字符串格式。

DateTime dFrom = DateTime.ParseExact("01-04-2019", "dd-MM-yyyy", CultureInfo.InvariantCulture);

DateTime dTo = DateTime.ParseExact("02-04-2019", "dd-MM-yyyy", CultureInfo.InvariantCulture);  

 var List = (from Receipt in DB.tbl_PurchaseReceipts 
             where DateTime.ParseExact(Receipt.Date, "dd-MM-yyyy", CultureInfo.InvariantCulture) >= dFrom && DateTime.ParseExact(Receipt.Date, "dd-MM-yyyy", CultureInfo.InvariantCulture) <= dTo
             select Receipt).ToList();

獲取錯誤:

方法“System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)”不支持轉換為 SQL。

您應該能夠手動解析這些部分並構建一個新的DateTime

var List = (from Receipt in DB.tbl_PurchaseReceipts 
            let rDate = new DateTime(Convert.ToInt32(Receipt.Date.Substring(6,4)),Convert.ToInt32(Receipt.Date.Substring(0,2)),Convert.ToInt32(Receipt.Date.Substring(3,2)))
            where dFrom <= rDate && rDate <= dTo
            select Receipt)
           .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM