0

我有一个包含开始/结束日期的对象列表。如果当前记录存在,我试图找到当前记录,或者找不到最近过期的记录。

  • 当前定义为过去的非空开始日期和空结束日期(开放式结束)或未来的结束日期。
  • 最近过期的可以定义为当前时间段没有最大结束日期。理论上,每个对象表示的时间跨度不应重叠,因此我希望最大结束日期足以满足最近过期的时间。

我试图按照这个回答来回答一个类似的问题,但是当列表中还有其他条目是未来的日期时,我似乎找不到正确的条目(即有一个开始/结束日期,两者都在未来) )。

我的尝试如下,其中 a 是开始日期,b 是结束日期,c 是我在调试时使用的指示器,d 用于定义列表条目类型。我只对类型 2 感兴趣。

   [TestMethod]
    public void TestRetrievalOfListElements()
    {

        var baseDate = DateTime.Now;

        var list = new[]
        {
             new { a = new DateTime?(baseDate.AddDays(-90)), 
                   b = new DateTime?(baseDate.AddDays(-60)), c = "LongExpired", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-190)), 
                   b = new DateTime?(baseDate.AddDays(-160)), c = "LongestExpired", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-59)), 
                   b = new DateTime?(baseDate.AddDays(+20)), c = "Current", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-159)), 
                   b = new DateTime?(baseDate.AddDays(-91)), c = "LongerExpired", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-90)), 
                   b = new DateTime?(baseDate.AddDays(-60)), c = "LongExpired", d=2 },
             new { a = new DateTime?(baseDate.AddDays(-190)), 
                   b = new DateTime?(baseDate.AddDays(-160)), c = "LongestExpired", d=2 },
             new { a = new DateTime?(baseDate.AddDays(-59)), 
                   b = new DateTime?(baseDate.AddDays(+20)), c = "Current", d=2 },
             new { a = new DateTime?(baseDate.AddDays(-159)), 
                   b = new DateTime?(baseDate.AddDays(-91)), c = "LongerExpired", d=2 },
             new { a = new DateTime?(baseDate.AddDays(+21)), 
                   b = new DateTime?(baseDate.AddDays(+60)), c = "Future", d=2 },

        }.ToList();

        // The following isn't really right either as it doesn't take into account
        // whether d is of type 1 or 2 either. Not sure how to combine aggregates and
        // other conditions ie MIN and of type 2

        var oldestEntry = (from x in list
                          where x.a == list.Min(d => d.a) &&
                          x.d == 2
                          select x).FirstOrDefault();

        Assert.IsTrue(oldestEntry.a == baseDate.AddDays(-190), "Expected oldest date to be 190 days earlier than today");

        var latestEntryThatIsntInTheFuture = 
                          (from x in list
                          from y in list.Where(z => z.d == 2 && z.a <= baseDate)
                          where x.a == list.Max(d => d.a) &&
                          x.d == 2
                          select x).FirstOrDefault();

        Assert.IsTrue(latestEntryThatIsntInTheFuture.a == baseDate.AddDays(-59), "Expected latest date that isn't in the future to be 59 days earlier than today");



    }
4

1 回答 1

1

这可能是您正在寻找的:

// Find the current which is defined as a non null start date that is in the past 
// and either a null end date (open ended) or and end date in the future. 
var now = DateTime.Now;
var current = list
    .Where(x => x.a.HasValue && x.a.Value < now && (!x.b.HasValue || x.b.Value > now))
    .FirstOrDefault();
if (current == null)
{ 
    //Then find most recently expired 
    current = list.Where(x => x.b.HasValue)
                  .OrderByDescending(x => x.b)
                  .FirstOrDefault();
}

因此,在我看来,您不需要在查询中进行聚合。

于 2012-07-12T07:53:03.853 回答