LINQ DateTimeOffset 与今天的比较

发布于 2024-09-27 08:18:01 字数 380 浏览 2 评论 0原文

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

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

最终是它们的集合:

IEnumerable<Sample> collection;

2 个问题:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屋檐 2024-10-04 08:18:01
// 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));
// 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));
溇涏 2024-10-04 08:18:01
var list1 = 
    collection.Where
        (c => c.expires.DateTime > DateTime.Now && 
              c.expires.DateTime < DateTime.Today.AddDays(1));

var list2 = 
    collection.Where
        (c => c.expires.DateTime >= DateTime.Now && 
              c.expires.DateTime <= DateTime.Now.AddHours(24));
var list1 = 
    collection.Where
        (c => c.expires.DateTime > DateTime.Now && 
              c.expires.DateTime < DateTime.Today.AddDays(1));

var list2 = 
    collection.Where
        (c => c.expires.DateTime >= DateTime.Now && 
              c.expires.DateTime <= DateTime.Now.AddHours(24));
走过海棠暮 2024-10-04 08:18:01

为了性能而“缓存”计算值是一个很好的做法,否则它将在每个循环中进行计算(如内部循环的位置):

DateTime tomorrow = DateTime.Today.AddDays(1);
DateTime now = DateTime.Now;
DateTime next24hrs = now.AddHours(24);
IEnumerable<Sample> next24HoursSamples = collection.Where(sample=>sample.expires>now && sample.expires<next24hrs).ToList();
IEnumerable<Sample> sameDaySamples = next24HoursSamples.Where(sample=>sample.expires>=now && sample.expires<tomorrow).ToList();

请注意,sameDay 列表是从已过滤的列表中检索的(same day 是接下来的 24 小时),因此需要过滤的项目较少。

编辑:我更新了代码以使用立即查询执行模型,因为 @danijels 警告延迟执行。

It's a good practice to "cache" the calculated values for performance, otherwise it would be calculated in each loop(as Where does a loop internally):

DateTime tomorrow = DateTime.Today.AddDays(1);
DateTime now = DateTime.Now;
DateTime next24hrs = now.AddHours(24);
IEnumerable<Sample> next24HoursSamples = collection.Where(sample=>sample.expires>now && sample.expires<next24hrs).ToList();
IEnumerable<Sample> sameDaySamples = next24HoursSamples.Where(sample=>sample.expires>=now && sample.expires<tomorrow).ToList();

Notice that the sameDay list is retrieved from the already filtered list(same day is a subset of the next 24 hours),so there are less items to filter on.

EDIT: I updated the code to use an immediate query execution model, as @danijels warns about deferred execution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文