• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Get different results with the same query in T-SQL and LINQ (EF Core)

Resolved: Get different results with the same query in T-SQL and LINQ (EF Core)

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m trying to retrieve records from a database in Azure and for the checks I run T-SQL queries directly and then pass them to LINQ against the EF Core context, but I’m running into this problem.
t-sql
When I filter for the records with FechaOrientativa greater than or equal to Today, the Estudiar field is equal to ‘pt’, I get 2,296 records.
Now in Angular, I do http.Get to my Web API where I execute the following:
And to my surprise, I receive only 2,151 records and I can’t find an explanation.
Any idea, please?
Thanks.

Answer:

Compare both SQL query and EF LINQ query, the difference is:
SQL – CAST( GETDATE() AS Date ) returns Today Date without time.
EF LINQ – DateTime.Now returns current Date Time.
Hence the queried result will be different
(Example: Only queried records with the date-time field equal/after the query date-time).
From Date and time functions, you are looing for DateTime.Today
DateTime.Today CONVERT(date, GETDATE())

Or you can use the SQL query in EF LINQ with .FromSqlRaw().

If you have better answer, please add a comment about this, thank you!

asp.net-core-webapi entity-framework-core linq sql tsql
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Vaadin 14.9 – Redirect on Session Destroy Event

26/03/2023

Resolved: How to separate multiple answers in one column, for multiple columns, by creating extra columns

26/03/2023

Resolved: How to set consistent decimal separators in R data frame?

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.