2

我上次使用的 Sum 和 Max 调用和使用计数是错误的。它们始终是存储在当前设置为 0 的第二个映射值中的值。我可以将其设为 -100,这就是 reduce 结果。我注意到有人说你应该把它做成一个数组,但我不明白这有什么帮助。我有一个使用 map/reduce 的对象的 linq 表达式,假设这两个映射联合在一起,它可以正常工作。

这是数据=> https://gist.github.com/940ccca1b0f8917e9eaf

我想支持这样的查询

        var stats = Session.Query<MultiMapApiKeyStats.ApiKeyStats, MultiMapApiKeyStats>()
            .Customize(x => x.WaitForNonStaleResultsAsOfNow())
            .Where(x => x.AccountId == Account.Id)
            .OrderBy(x => x.Key).ToList();

并建立一个像 桌子

 public MultiMapApiKeyStats()
        {
            AddMap<KeyUsageBase>(uses => from use in uses
                                         select new
                                         {
                                             AccountId = use.AccountId,
                                             ApiKeyId = use.ApiKeyId,
                                             Key = (string)null,
                                             UsageCount = 1,
                                             LastUsed = use.LastUsedTicks,
                                             Pattern = (string)null,
                                             Status = ApiKey.KeyStatus.None,
                                             Type = ApiKey.ApplicationType.None
                                         });
            AddMap<ApiKey>(keys => from key in keys
                                   select new
                                          {
                                              AccountId = key.AccountId,
                                              ApiKeyId = key.Id,
                                              Key = key.Key,
                                              UsageCount = 0,
                                              LastUsed = 0,
                                              Pattern = key.Pattern,
                                              Status = key.ApiKeyStatus,
                                              Type = key.Type
                                          });


            Reduce = results => from result in results
                                group result by result.ApiKeyId
                                    into g
                                    select new
                                           {
                                           AccountId = g.Select(x => x.AccountId).FirstOrDefault(),
                                           ApiKeyId = g.Key,
                                           Key = g.Select(x => x.Key).FirstOrDefault(x => x != null),
                                           UsageCount = g.Sum(x => x.UsageCount),
                                           LastUsed = g.Max(x => x.LastUsed),
                                           Pattern = g.Select(x => x.Pattern).FirstOrDefault(),
                                           Status = g.Select(x => x.Status).FirstOrDefault(),
                                           Type = g.Select(x => x.Type).FirstOrDefault()
                                           };
4

1 回答 1

2

索引错误sequence contains no matching elements是调用First空集合的结果,这就是您应该使用FirstOrDefault. 此外,在 map 和 reduce 声明中引用您自己的类会导致错误,因为 RavenDB 无法在索引时解析这些类型。

于 2012-09-20T16:26:39.310 回答