Filter for the next 90 days ASP.NET MVC

Asked

Viewed 342 times

1

need to make a filter with Line in an entity, this filter should bring all events for the next 90 days.

Today’s example: 11/01/2017 until 11/04/2017 (90 days)

_con.SiteContexto.AsNoTracking()
.Where(e => e.Data....).ToList();

2 answers

6


You can do it like this:

DateTime filtro = DateTime.Now.AddDays(90);

var r = db.SiteContexto.Where(x => x.DataEnvio < filtro && x.DataEnvio > DateTime.Now).ToList();

Get all records who have the date greater than now and less than (now + 90 days)

UPDATING:

Improving the explanation a little:

Datetime.Now will return the current date and time. To mount with the last minute of the day and not the first you can do this:

DateTime filtro = DateTime.Now.Date.AddDays(90)
                              .AddHours(23)
                              .AddMinutes(59)
                              .AddSeconds(59)
                              .AddMilliseconds(999);

With that the < filtro will recover all of the last day too.

You can vary with this, how to add 91 days (.AddDays(91)) instead of adding as suggested above.

Thanks for the comment jbueno.

  • Watch out for the hours!

  • Thanks for the comment, I improved the response a little!

  • DateTime.Now will return the current time and not with "zero hours".

3

Get all events with date equal to or greater than today and less than or equal to (today + 90 days).

It is important to use the function TrucateTime() to disregard the hours.

_con.SiteContexto
    .Where(e => DbFunctions.TrucateTime(e.Data) >= DbFunctions.TrucateTime(DateTime.Now) &&
                DbFunctions.TruncateTime(e.Data) <= DbFunctions.TrucateTime(DateTime.Now.AddDays(90))
    .ToList();

If you’re using an Entity Framework version earlier than 6, use EntityFunctions instead of DbFunctions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.