.NET CORE LinQ查询中计算时间差(Calculation time difference in. Net core LINQ query)

项目中有这样一个需求:

当客户端输入一个数值,后端需要过滤两个时间差的秒数是该数值再返回数据

1错误的方法

一开始直接使用了C#中的时间计算方法

int second=20;
if (second > 0)
  {
     list = list.Where(p => (p.EndTime - p.StartTime).TotalSeconds >= second);
  }

以上使用TotalSeconds 前端获取数据时候就会报错。

2在 .NET Framework 中我可以通过 DbFunctions.DiffDays 来实现此需求

3在 .NET Core 中我可以使用 SQL 提供的原生函数EF.Functions.DateDiffSecond

int second=20;
if (second > 0)
  {
     list = list.Where(p => EF.Functions.DateDiffSecond(p.StartTime,p.EndTime) >= second);
  }

EF.Functions还有许多方法,计算天数,小时数,分钟数等等。。。

官方文档

————————

There is a requirement in the project:

When the client enters a value, the back-end needs to filter the seconds of the two time differences, and then return the data

1 wrong method

At the beginning, the time calculation method in c# was directly used

int second=20;
if (second > 0)
  {
     list = list.Where(p => (p.EndTime - p.StartTime).TotalSeconds >= second);
  }

An error will be reported when using the totalseconds front end to obtain data.

2在 .NET Framework 中我可以通过 DbFunctions.DiffDays 来实现此需求

3在 .NET Core 中我可以使用 SQL 提供的原生函数EF.Functions.DateDiffSecond

int second=20;
if (second > 0)
  {
     list = list.Where(p => EF.Functions.DateDiffSecond(p.StartTime,p.EndTime) >= second);
  }

EF. Functions also has many methods to calculate days, hours, minutes and so on…

Official documents