41

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.

3
  • 1
    Do you calculations outside of the lambda statement. When it's inside the lambda like you're using it, you're trying have EntityFramework do the calculation when it's not meant to do so.
    – Middas
    Mar 7, 2013 at 6:14
  • So LINQ to SQL won't "translate" an expression like product.EventDate.Subtract(DateTime.Now). In C# an alternative notation is product.EventDate - DateTime.Now where one uses an overload of the minus (-) operator. Does anyone know if that will work with LINQ to SQL? After all, subtraction of dates is also possible in SQL dialects. Mar 7, 2013 at 6:47
  • @JeppeStigNielsen For EF you can use EntityFunctions.DiffDays
    – scartag
    Mar 7, 2013 at 7:09

5 Answers 5

63

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)
1
  • 21
    EntityFunctions is now Obsolete: use System.Data.Entity.DbFunctions instead.
    – celerno
    Mar 7, 2014 at 23:27
42

The simplest approach is to work out the bounds before you perform the query:

// Only evaluate DateTime.Now once for consistency. You might want DateTime.Today instead.
DateTime now = DateTime.Now;
DateTime nowPlus60Days = now.AddDays(60);
DateTime nowPlus30Days = now.AddDays(30);
DateTime nowPlus20Days = now.AddDays(20);

var query = ...
            where product.EventDate <= nowPlus60Days
            ...

Note that your current query doesn't even really make sense, as each "or"'d clause is stating that the given computation is both less than or equal to a value and greater than or equal to the same value. If you want simple "equal to" then use that. If not, it's not clear what you are trying to do.

If you're trying to bucket the values into "less than 20", "20-30", "30-60", "more than 60" you'll need to use grouping of some form.

5

This should work:

using System.Data.Entity.SqlServer;

where (int)SqlFunctions.DateDiff("day", product.EventDate, DateTime.Now) <= 60
3

To add on to scartag's answer,

The MSDN documentation for DbFunctions.DiffDays doesn't directly mention whether and when the value returned by DiffDays() will be negative, so I thought I'd provide that information here:

The result will be negative when the argument 1 date is larger than (i.e. in the future relative to) the argument 2 date.

For example, given a table Deliveries with a non-null field ScheduledDeliveryDate that can have values both in the past and in the future relative to the current date, this query will get all records with a delivery date/time within 2 days of the current date/time (both past and future):

DateTime now = DateTime.Now;
var results = from d in Deliveries
    where (DbFunctions.DiffDays(d.ScheduledDeliveryDate, now) < 2
        && DbFunctions.DiffDays(d.ScheduledDeliveryDate, now) > -2)
    select d;
0

Count Total

 var CountDay = db.YourTable.Where(s => s.Condition1 == 1 && s.Condition2 == false && DbFunctions.DiffDays(s.YourDate, DateTime.Now) > 0).Count().ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.