LINQ Expression translates into under laying data source language, in your case its
SQL, and in LINQ to Entities
DateTime.AddMinutes doesn't have any implementation which would translate the query to corresponding equivalent in SQL. That is why .Net framework provided.
You should see: Date and Time Canonical Functions
You can have your query as:
var resultSet = ( from entity in _dbContext.EntityCollection where /*non-date-related where clause stuff*/ && entity.DateAdded <= System.Data.Objects.EntityFunctions.AddMinutes( DateTime.Now, -1 * timeElapsedThresholdInMinutes) select entity);
But in your case its better if you can calculate the value before hand, like in your second code sample, since that will avoid the conversion for every record.
I'd really like to understand why LINQ->Entities considers DateTime.Now.AddMinutes() as a .NET method that has no T-SQL equivalent
Because every mapping from .NET to T-SQL has to be manually implemented, and they just didn't implement this one...
EntityFunctions is obsolete (EF 6.x).
Please use DbFunctions class for DateTime manipulations.
e.g from cmd in context.Commands where cmd.ExecutedOn > DbFunctions.AddMilliseconds(baseDate, millisecondsToAdd) select cmd
I've been using the
DbFunction for long time, until today I realized...why do I even need to bother with that
DbFunction at all.
And here is what came to my mind:
var dateLimit = DateTime.Now.AddMinutes(-20); var result = db.TableName.Where(x => x.MyDateTime > dateLimit);
I just took the
'AddMinutes' operation out of the linq expression.
- Issues with DateTime.AddMinutes() in Dynamic LINQ Query
- Use Linq query to compare date only with DateTime field
- convert string to DateTime in linq query with where clause?
- How can I build Linq query with dynamic OR statements?
- LINQ TO SQL, Dynamic query with DATE type fields
- Dynamic linq query with multiple/unknown criteria
- linq query with dynamic predicates in where clause joined by OR
- Using LINQ Dynamic Query Library with Dictionary<string, object> and .AsQueryable()
- How can I create a dynamic LINQ query in C# with possible multiple group by clauses?
- Dynamic LINQ (to entities) Where with nullable DateTime column
- How to build a nested query with the dynamic LINQ library
- How to dynamic add filters to a LINQ query against an Odata Source in C#
- Retrieve and print data from dynamic sql query with linq
- Dynamic query with LINQ won't work
- Convert String to DateTime in LINQ query with Entity Framework Core
- Query dynamic data with LINQ
- Linq sub query when using a repository pattern with EF code first
- MVC Linq Query with dynamic column name in WHERE clause
- Why is my Linq Oracle DB query not comparing timestamp with datetime correctly?
- Creating a compiled query linq and performance issues with regular LINQ
- LINQ Query Field<T> with dynamic type
- Need help with a LINQ Query using the Dynamic LINQ Library
- LINQ Dynamic Query with group by and string variable
- Dynamic linq query with nested groups
- Implementing Dynamic LINQ Query with "NOT IN" Clause
- LINQ Query with dynamic number of boolean ANDs
- Avoiding repeating code with Linq query + optional params
- Linq Query with dynamic type
- Linq query with Datetime Object
- Linq query to db with dynamic where parameters
- Asp.net MVC4: Custom paging
- Convert SQL to Linq queries in Entity Framework
- Single enumeration with multiple filters
- Performing a date comparison with a date field stored in the database
- c# Query overlapping date
- LINQ To Entities Include + Where Method
- Linq Query Using Not In A Separate Table
- How to aggregate a collection of items in one cell of an ASPxGridView
- How can I detect "missing" elements in an IEnumerable<T>?
- How to calculate STD dev and Average with linq and entity framework at the same time
- why doesn't .Except() and Intersect() work here using LINQ?
- LINQ to Entities similar code
- Use LINQ to compare the two lists
- How do I remove elements from a table?
- Why is mongo return zero matching document although there is one?
- Associate values of one list with the ones of another list with linq
- OrderBy list for only Date but not Time
- How to use Any between two ienumerables
- LINQ query error on Inner Join
- Moving node in xdocument