LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method

Ragesh S picture Ragesh S · Mar 7, 2013 · Viewed 31.6k times · Source

I try to select records in database in 60 days 30 days 20 days differents in current date.

Please see this query in below.

 var uploads = (
                from files in _fileuploadRepository.Table
                join product in _productRepository.Table on files.Event equals product.Id
                where
                    (
                product.EventDate != null &&
                    (product.EventDate.Subtract(DateTime.Now).Days <= 60 && product.EventDate.Subtract(DateTime.Now).Days >= 60) ||
                    (product.EventDate.Subtract(DateTime.Now).Days <= 30 && product.EventDate.Subtract(DateTime.Now).Days >= 30) ||
                    (product.EventDate.Subtract(DateTime.Now).Days <= 20 && product.EventDate.Subtract(DateTime.Now).Days >= 20))
                    &&
                files.IsSkiped == false
                select files;
            ).ToList();

But a error occurred this query.

enter image description here

I am clueless. Please Help.

Answer

scartag picture scartag · Mar 7, 2013

You could use the EntityFunctions.DiffDays method

EntityFunctions.DiffDays(product.EventDate, DateTime.Now) //this will return the difference in days

UPDATE

EntityFunctions is now obsolete so you should use DBFunctions instead.

System.Data.Entity.DbFunctions.DiffDays(product.EventDate, DateTime.Now)