DateTime difference in Entity Framework Core

entity-framework-core

Question

I have the following query on Entity Framework Core:

public class Question {
  public DateTime? Approved { get; set; }
  public DateTime Created { get; set; }
}

public class QuestionModel {
  public TimeSpan ResponseTime { get; set; }
}

List<Question> questions = await context.Questions
  .Select(x =>
    new QuestionModel {
      ResponseTime = x.Approved.Value - x.Created
    }).ToListAsync();

But I get the following error:

System.Data.SqlClient.SqlException: Operand data type datetime2 is invalid for subtract operator.

How can I get a DateTime difference with Entity Framework Core?

1
1
10/14/2016 6:00:18 PM

Popular Answer

You can use too this extension method Subtract()

List<Question> questions = (await context.Questions.ToListAsync())
.Select(x => new QuestionModel {
ResponseTime = x.Approved.Value.Subtract(x.Created);
})
.ToList();
4
12/5/2016 1:33:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow