按两个日期字段排序,以较新的日期字段为准

发布于 2024-10-21 10:03:55 字数 433 浏览 2 评论 0原文

我希望我的对象按最新日期排序,可以按更新日期或创建日期(以较新的日期为准)。下面我的代码的问题是未更新的对象(为空)出现在底部。

public IQueryable<MyObject> GetMyObjects()
{
    return from obj in _db.MyObjects
           orderby obj.UpdatedDate descending, obj.CreatedDate descending
           select obj;
}

即使某个对象没有更新日期,它仍然应该显示在其他有更新日期的对象之前,前提是这些对象的更新日期小于该对象的创建日期。我怎样才能实现这个目标?基本上,我想按最新日期排序,并使用更新日期或创建日期(以较晚者为准)。

我的 linq 代码应该如何更改才能实现此目的?谢谢!

I want my objects ordered by latest date, either by updated date or created date, whichever one is more recent. The problem with my code below is that objects that are not updated (which are null) appear at the bottom.

public IQueryable<MyObject> GetMyObjects()
{
    return from obj in _db.MyObjects
           orderby obj.UpdatedDate descending, obj.CreatedDate descending
           select obj;
}

Even if an object doesn't have an updated date, it should still appear before other objects that do have an updated date, if those objects' updated dates are less that this object's created date. How can I achieve this? Basically, I want to order by latest date and use either the updated date or created date whichever one is later.

How should my linq code be changed to achieve this? Thanks!

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

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

发布评论

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

评论(2

℉服软 2024-10-28 10:03:55

我不知道我是否很好地理解了您的要求,但这是我的解决方案:

return from myclass in classes
       let updated = myclass.Updated ?? myclass.Created
       orderby updated descending
       select myclass;

使用名为 MyClass 的类进行测试:

class MyClass
{
    public DateTime? Updated { get; set; }
    public DateTime Created { get; set; }
}

并使用这些值:

MyClass[] classes = 
{
    new MyClass() { Created = DateTime.Now.AddDays(12)},
    new MyClass() { Created = DateTime.Now.AddDays(-5), Updated = DateTime.Now.AddDays(3)},
    new MyClass() { Created = DateTime.Now},
    new MyClass() { Created = DateTime.Now.AddDays(2), Updated = DateTime.Now.AddDays(10)},
    new MyClass() { Created = DateTime.Now},
    new MyClass() { Created = DateTime.Now.AddDays(1), Updated = DateTime.Now.AddDays(1)},
    new MyClass() { Created = DateTime.Now},
    new MyClass() { Created = DateTime.Now.AddDays(20), Updated = DateTime.Now.AddDays(4)}
};

结果是:

Updated = null - Created = 22/03/2011
Updated = 20/03/2011 - Created = 12/03/2011
Updated = 14/03/2011 - Created = 30/03/2011
Updated = 13/03/2011 - Created = 05/03/2011
Updated = 11/03/2011 - Created = 11/03/2011
Updated = null - Created = 10/03/2011
Updated = null - Created = 10/03/2011
Updated = null - Created = 10/03/2011

I don't know if I have well understood what you're asking but here is my solution:

return from myclass in classes
       let updated = myclass.Updated ?? myclass.Created
       orderby updated descending
       select myclass;

Tested with a class named MyClass:

class MyClass
{
    public DateTime? Updated { get; set; }
    public DateTime Created { get; set; }
}

And with these values:

MyClass[] classes = 
{
    new MyClass() { Created = DateTime.Now.AddDays(12)},
    new MyClass() { Created = DateTime.Now.AddDays(-5), Updated = DateTime.Now.AddDays(3)},
    new MyClass() { Created = DateTime.Now},
    new MyClass() { Created = DateTime.Now.AddDays(2), Updated = DateTime.Now.AddDays(10)},
    new MyClass() { Created = DateTime.Now},
    new MyClass() { Created = DateTime.Now.AddDays(1), Updated = DateTime.Now.AddDays(1)},
    new MyClass() { Created = DateTime.Now},
    new MyClass() { Created = DateTime.Now.AddDays(20), Updated = DateTime.Now.AddDays(4)}
};

The result is:

Updated = null - Created = 22/03/2011
Updated = 20/03/2011 - Created = 12/03/2011
Updated = 14/03/2011 - Created = 30/03/2011
Updated = 13/03/2011 - Created = 05/03/2011
Updated = 11/03/2011 - Created = 11/03/2011
Updated = null - Created = 10/03/2011
Updated = null - Created = 10/03/2011
Updated = null - Created = 10/03/2011
深陷 2024-10-28 10:03:55

我在 LINQPad 中使用 Northwind DB 尝试了这个查询,效果很好:

from o in Orders
orderby o.RequiredDate > o.ShippedDate ? o.RequiredDate : o.ShippedDate
select new
{
    o.RequiredDate,
    o.ShippedDate
}

I tried this query in LINQPad with the Northwind DB, and it worked fine:

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