简体   繁体   English

C# Linq 如果日期以字符串格式存储在数据库中,则 2 个日期之间的日期

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

I want to get data using c# linq between two dates but the date stored in the database is 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();

Getting Error:获取错误:

Method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' has no supported translation to SQL.方法“System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)”不支持转换为 SQL。

You should be able to parse out the parts manually and build a new DateTime :您应该能够手动解析这些部分并构建一个新的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