Linq:TimeSpan中2个DateTimes之间的差异

时间:2021-06-18 21:28:29

I'm trying to do this:

我正在尝试这样做:

Tickets.Where(t => (t.Date - myTicket.Date) < TimeSpan.FromSeconds(120));

I'm getting the "DbArithmeticExpression arguments must have a numeric common type" error. How can I do this, considering that I need the difference in a TimeSpan?

我得到“DbArithmeticExpression参数必须有一个数字公共类型”错误。考虑到我需要在TimeSpan中有所不同,我该怎么做?

Thanks in advance.

提前致谢。

3 个解决方案

#1


26  

You would want to use SqlFunctions.DateDiff

您可能希望使用SqlFunctions.DateDiff

Tickets.Where(t => 
      SqlFunctions.DateDiff("second", t.Date, myTicket.Date) < 120));

#2


4  

You can also use this;

你也可以用这个;

var result = db.Tickets.Where(t => 
       SqlMethods.DateDiffSecond(myTicket.Date ,  t.Date) < 120);

#3


2  

Arithmetic with DateTime is not supported in Entity Framework. You have to use one of the SqlFunctions. So, for your statement, something like:

实体框架中不支持使用DateTime进行算术运算。您必须使用其中一个SqlFunction。所以,对于你的陈述,例如:

Tickets.Where(t => 
      SqlFunctions.DateDiff("second", t.Date, myTicket.Date) < 120));

#1


26  

You would want to use SqlFunctions.DateDiff

您可能希望使用SqlFunctions.DateDiff

Tickets.Where(t => 
      SqlFunctions.DateDiff("second", t.Date, myTicket.Date) < 120));

#2


4  

You can also use this;

你也可以用这个;

var result = db.Tickets.Where(t => 
       SqlMethods.DateDiffSecond(myTicket.Date ,  t.Date) < 120);

#3


2  

Arithmetic with DateTime is not supported in Entity Framework. You have to use one of the SqlFunctions. So, for your statement, something like:

实体框架中不支持使用DateTime进行算术运算。您必须使用其中一个SqlFunction。所以,对于你的陈述,例如:

Tickets.Where(t => 
      SqlFunctions.DateDiff("second", t.Date, myTicket.Date) < 120));