简体   繁体   English

EF4中的日期差

[英]date difference in EF4

i need to get a difference of two dates ,one date from a table and one the current date, and the difference should be less than 9 0days. 我需要得到两个日期之间的差额,一个日期从表中得到一个日期,而一个当前日期,并且该差应小于9 0days。 i need to use this as filter in where clause of the linq i tried doing this 我需要在linq的where子句中使用它作为过滤器,我尝试这样做

var list = from p in context.persons where ((p.CreateDT).Subtract(DateTime.Now).Days < 90) select p; var list =从context.persons中的p中选择((p.CreateDT).Subtract(DateTime.Now).Days <90)选择p;

i get this excpetion : 我得到这个称呼:

LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method, and this method cannot be translated into a store expression. LINQ to Entities无法识别方法'System.TimeSpan Subtract(System.DateTime)',并且该方法无法转换为商店表达式。

I did research other articles but nothing helped..Any ideas 我曾研究过其他文章,但无济于事。

Trick here is that it can't translate all your fancy oo hoo-ha to plain old sql. 这里的技巧是它不能将所有您喜欢的东西转换为普通的旧sql。 Trick is to flip it on it's head: 绝招是将其翻转:

var before = DateTime.Now.AddDays(-90);
var persons = context.Persons.Where(x => x.CreateDT > before);

EXPLANATION 说明

Remember that everything in the WHERE bit of your LINQ statement must be translated from C# to SQL by the EF. 请记住,LINQ语句的WHERE位中的所有内容都必须由EF从C#转换为SQL。 It is very, very capable out of the box and handles most basic tasks, but it has no idea how to understand the most rudimentary method calls, such as DateTime.Subtract(). 它非常强大,可以处理大多数基本任务,但是却不知道如何理解最基本的方法调用,例如DateTime.Subtract()。 So the idea here is to let it do what it does best by precalculating a value and then passing that to the data tier. 因此,这里的想法是通过预先计算一个值,然后将其传递给数据层,使其发挥最大作用。

The first line subtracts 90 days from the current time by adding negative 90 days. 第一行通过添加负90天从当前时间中减去90天。 The second line passes it off to the database server. 第二行将其传递给数据库服务器。

The second line should translate to the SQL WHERE CreateDT > @BEFORETHIS 第二行应转换为SQL WHERE CreateDT> @BEFORETHIS

Update 更新资料

It seems that EF doesn't support subtracting dates and returning a TimeSpan. 似乎EF不支持减去日期和返回TimeSpan。 Here's one way to solve the problem: 这是解决问题的一种方法:

DateTime oldestDate = DateTime.Now.AddDays(-90); 
var list = from p in context.persons 
           where p.CreateDT >= oldestDate
           select p;

See this thread on Stackoverflow. 请参阅Stackoverflow上的线程。

I think you have to use EntityFunctions.DiffDays to calculate in your query. 我认为您必须在查询中使用EntityFunctions.DiffDays进行计算。 see: How do I get Age distribution list by grouping 请参阅: 如何通过分组获得年龄分布列表

Try doing simply (p.CreateDate - DateTime.Now).Days < 90 . 尝试简单地做(p.CreateDate - DateTime.Now).Days < 90 Instead of calling DateTime.Subtract(). 而不是调用DateTime.Subtract()。 In some cases the operator overloads are implemented for Entity Framework even when the corresponding named methods are not. 在某些情况下,即使没有相应的命名方法,也为Entity Framework实现了操作符重载。

If that doesn't work you could instead use ESQL or a stored procedure. 如果那不起作用,您可以改用ESQL或存储过程。 As a final, dirty solution, you could call context.persons.ToList() and then call the DateTime.Subtract(). 作为最终的肮脏解决方案,您可以调用context.persons.ToList() ,然后调用DateTime.Subtract()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM