Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ DateTimeOffset comparison with today

I have a class with a DateTimeOffset property:

public class Sample 
{
    public DateTimeOffset expires { get; set; }
}

and eventually a collection of them:

IEnumerable<Sample> collection;

2 questions:

  1. What is the best way to create a method that returns all the Sample items from the collection where expires is greater than now and is still today (i.e. before midnight)?

  2. What is the best way to return all Sample items from the collection where expires is in the next 24 hours?

like image 555
Chris Avatar asked Oct 28 '25 20:10

Chris


1 Answers

// greater than now, still today            
collection.Where(d => d.expires.DateTime > DateTime.Now && d.expires.Date == DateTime.Today);

// expires in the next 24 hours
collection.Where(d => d.expires.DateTime > DateTime.Now && d.expires.DateTime < DateTime.Now.AddHours(24));
like image 158
danijels Avatar answered Oct 31 '25 13:10

danijels