在LINQ Query中将字符串转换为datetime并检查今天的日期(In LINQ Query convert the string to datetime and check with toda

编程入门 行业动态 更新时间:2024-10-24 00:17:53
在LINQ Query中将字符串转换为datetime并检查今天的日期(In LINQ Query convert the string to datetime and check with today's date)

在表中,DueDate数据类型是varchar。 现在我想查看今天的截止日期

var query = (from o in db.Order_Reports where Convert.ToDateTime(o.ReportDueDateTime) >= DateTime.Now select o);

错误

LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法,并且此方法无法转换为商店表达式。

我想如何将字符串转换为日期和时间并检查今天的日期

In Table, DueDate data type is varchar. Now I want to check the due date with today's date

var query = (from o in db.Order_Reports where Convert.ToDateTime(o.ReportDueDateTime) >= DateTime.Now select o);

The error

LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method, and this method cannot be translated into a store expression.

How am i suppose to convert the string to date and time and check with today's date

最满意答案

我不知道如你所描述的那样实现它的方法,因为

SQL中没有函数可以通过不等式过滤字符串格式的日期(至少那些与日期格式一起使用并且可以从LINQ转换为SQL查询的日期)。 您的数据库设计存在缺陷,导致“正常”方法无法正常工作

但是,有一些可能的解决方法

修改数据库,使日期为实际日期时间格式(最佳选项,但当然有时不可用) 创建存储过程以实现相同的目标。 在SQL代码中,您可以使用日期解析 查询修改如下:

(码)

// Our required date DateTime reportDate = new DateTime(2014,1,1).Date; // Let's find number of days between now and required day. Ensure that the date is not in the future! int deltaDays = (DateTime.Now.Date - date).Days; // Let's get the list of dates which we need the reports for var dates = Enumerable.Range(0, deltaDays + 1).Select(dd => DateTime.Now.Date.AddDays(-dd).ToString("MM/dd/yyyy")).ToArray(); // and query by this list var query = (from o in db.Order_Reports where o.ReportDueDateTime in dates select o);

这样效率会有点低,但可以在不更改数据库的情况下达到目的。 将其视为临时解决方案。

I don't know a way to implement it as you describe it, because

There's no function in SQL that will filter the dates in string format by inequality (at least those that work with your date format and are convertable from LINQ to SQL query). You have a flaw in your database design which prevents "normal" methods from working

However, there are some possible workarounds

Amend the database so that the date is of actual datetime format (best option, but of course sometimes unavailable) Create a stored procedure to achieve the same goal. In the SQL code, you can use date parsing Amend the query as follows:

(code)

// Our required date DateTime reportDate = new DateTime(2014,1,1).Date; // Let's find number of days between now and required day. Ensure that the date is not in the future! int deltaDays = (DateTime.Now.Date - date).Days; // Let's get the list of dates which we need the reports for var dates = Enumerable.Range(0, deltaDays + 1).Select(dd => DateTime.Now.Date.AddDays(-dd).ToString("MM/dd/yyyy")).ToArray(); // and query by this list var query = (from o in db.Order_Reports where o.ReportDueDateTime in dates select o);

This will be a little inefficient, but achieve the purpose without changing the DB. Treat it as temporary solution.

更多推荐

本文发布于:2023-08-06 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   中将   日期   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!