我有一个多地图索引,需要在 Reduce 结果中获取最大日期。
当我尝试:
Reduce = results => from result in results
group result by result.Id into g
select new
{
Id = g.Key,
AccountId = g.Select(x => x.AccountId).Where(x => x != null).FirstOrDefault(),
Hostnames = g.SelectMany(x => x.Hostnames),
NextRenewalDate = g.Select(x => x.NextRenewalDate).Max(),
SubscriptionCount = g.Sum(x => x.SubscriptionCount)
};
NextRenewalDate
始终为空。
但是,我可以这样做:
NextRenewalDate = g.Select(x => x.NextRenewalDate).FirstOrDefault,
我确实得到了结果。
我想知道这是否是由于 RavenDB 如何解释索引中的日期。我的减少结果类如下:
public class ReduceResult
{
public string Id { get; set; }
public string AccountId { get; set; }
public string[] Hostnames { get; set; }
public DateTime NextRenewalDate { get; set; }
public int SubscriptionCount { get; set; }
}
更新
从那以后,我就能够使索引正常工作,但前提是我将其DateTime
转换为 ISO8601 字符串。我的完整索引如下:
public class Account_Sites : AbstractMultiMapIndexCreationTask<Account_Sites.ReduceResult>
{
public Account_Sites()
{
AddMap<CMS.Domain.Site>(sites => from site in sites
from url in site.Urls
select new
{
Id = site.Id,
AccountId = (string)null,
Hostnames = new[] { url.Hostname },
SubscriptionDueDate = DateTime.MinValue,
SubscriptionStartDate = DateTime.MinValue
});
AddMap<Domain.Account>(accounts => from account in accounts
from siteId in account.Sites
from subscription in account.Subscriptions
where subscription != null
select new
{
Id = siteId,
AccountId = account.Id,
Hostnames = new string[0],
SubscriptionDueDate = subscription.NextRenewalDate,
SubscriptionStartDate = subscription.StartDate
});
Reduce = results => from result in results
group result by result.Id into g
select new
{
Id = g.Key,
AccountId = g.Select(x => x.AccountId).Where(x => x != null).FirstOrDefault(),
Hostnames = g.SelectMany(x => x.Hostnames),
SubscriptionDueDate = g.Max(x => x.SubscriptionDueDate.ToString("o")), // ISO 8601 format e.g. 2012-10-22T12:51:03.0263843+00:00
SubscriptionStartDate = g.Max(x => x.SubscriptionStartDate.ToString("o"))
};
}
public class ReduceResult
{
public string Id { get; set; }
public string AccountId { get; set; }
public string[] Hostnames { get; set; }
public DateTime SubscriptionDueDate { get; set; }
public DateTime SubscriptionStartDate { get; set; }
}
}
更新 2
我在下面发布了一个完整的失败测试。丹尼尔的测试确实有效,但以下失败。但是,将 to更改TestSubscription.EndDate
为DateTime.Now
(而不是UtcNow
)确实有效。这是使用 build 1.2.2103-Unstable。
public class TestAccount
{
public string Id { get; set; }
public List<string> Sites { get; set; }
public List<TestSubscription> Subscriptions { get; set; }
public TestAccount()
{
Sites = new List<string>();
Subscriptions = new List<TestSubscription>();
}
}
public class TestSubscription
{
public DateTime EndDate { get; set; }
}
public class TestSite
{
public string Id { get; set; }
public string Hostname { get; set; }
}
public class SitesWithSubscriptions : AbstractMultiMapIndexCreationTask<SitesWithSubscriptions.Result>
{
public SitesWithSubscriptions()
{
AddMap<TestSite>(sites => from site in sites
select new
{
SiteId = site.Id,
Hostname = site.Hostname,
SubscriptionEndDate = DateTime.MinValue
});
AddMap<TestAccount>(accounts => from account in accounts
from siteId in account.Sites
from subscription in account.Subscriptions
select new
{
SiteId = siteId,
Hostname = (string)null,
SubscriptionEndDate = subscription.EndDate
});
Reduce = results => from result in results
group result by result.SiteId into g
select new
{
SiteId = g.Key,
Hostname = g.Select(x => x.Hostname).Where(x => x != null).FirstOrDefault(),
SubscriptionEndDate = g.Max(x => x.SubscriptionEndDate)
};
}
public class Result
{
public string SiteId { get; set; }
public string Hostname { get; set; }
public DateTime SubscriptionEndDate { get; set; }
}
}
public class SitesSpecs : RavenSpecs
{
static IEnumerable<SitesWithSubscriptions.Result> results;
Establish ctx = () =>
{
using (var session = Store.OpenSession())
{
var site = new TestSite { Hostname = "www.dev.com" };
session.Store(site);
var account = new TestAccount();
account.Subscriptions.Add(new TestSubscription { EndDate = DateTime.UtcNow });
account.Sites.Add(site.Id);
session.Store(account);
session.SaveChanges();
}
};
Because of = () =>
{
using (var session = Store.OpenSession())
{
results = session.Query<SitesWithSubscriptions.Result, SitesWithSubscriptions>()
.Customize(x => x.WaitForNonStaleResults())
.ToList();
}
};
It Should_return_sites_with_subscription_end_dates = ()
=>
{
results.Count().ShouldEqual(1);
results.First().SubscriptionEndDate.Date.ShouldEqual(DateTime.UtcNow.Date);
};
}