C# Linq:TimeSpan 中 2 个日期时间之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10214901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq: Difference between 2 DateTimes in TimeSpan
提问by Leila
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.
提前致谢。
采纳答案by Magnus
You would want to use SqlFunctions.DateDiff
你会想要使用SqlFunctions.DateDiff
Tickets.Where(t =>
SqlFunctions.DateDiff("second", t.Date, myTicket.Date) < 120));
回答by RajN
Arithmetic with DateTime is not supported in Entity Framework. You have to use one of the SqlFunctions. So, for your statement, something like:
实体框架不支持带日期时间的算术。您必须使用SqlFunctions 之一。因此,对于您的陈述,例如:
Tickets.Where(t =>
SqlFunctions.DateDiff("second", t.Date, myTicket.Date) < 120));
回答by ?a?atay Ay
You can also use this;
你也可以使用这个;
var result = db.Tickets.Where(t =>
SqlMethods.DateDiffSecond(myTicket.Date , t.Date) < 120);

