LINQ DateTimeOffset 与今天的比较

Chr*_*ris 4 c# linq linq-to-sql

我有一个带有 DateTimeOffset 属性的类:

public class Sample 
{
    public DateTimeOffset expires { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

最终是它们的集合:

IEnumerable<Sample> collection;
Run Code Online (Sandbox Code Playgroud)

2个问题:

  1. 创建一个从集合中返回所有样本项的方法的最佳方法是什么,其中过期时间大于现在并且仍然在今天(即午夜之前)?

  2. 从集合中退回将在未来 24 小时内过期的所有样品商品的最佳方式是什么?

dan*_*els 5

// 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));
Run Code Online (Sandbox Code Playgroud)