0

总而言之,我正在尝试将此位添加到 Raven Multi Map 查询中:

this.AddMap<Ownership>(ownerships => from o in ownerships
                                     select new
                                     {
                                         CardId = o.CardId,
                                         Expansion = (string)null,
                                         Owned = 1,
                                         Rarity = (string)null,
                                         Artist = Enumerable.Empty<string>(),
                                         Colors = Enumerable.Empty<string>(),
                                         ConvertedManaCost = Enumerable.Empty<decimal?>(),
                                         Name = Enumerable.Empty<string>(),
                                         Power = Enumerable.Empty<string>(),
                                         Text = Enumerable.Empty<string>(),
                                         Toughness = Enumerable.Empty<string>(),
                                         Types = Enumerable.Empty<string>(),
                                     });

Raven 将该表达式转换为以下表达式(为便于阅读而格式化):

this.ForEntityNames.Add("Ownerships");
this.AddMapDefinition(docs => docs
    .Where(__document => __document["@metadata"]["Raven-Entity-Name"] == "Ownerships")
    .Select((Func<dynamic, dynamic>)(o => new
    {
        CardId = o.CardId,
        Expansion = (System.String)null,
        Owned = 1,
        Rarity = (System.String)null,
        Artist = Enumerable.Empty(),
        Colors = Enumerable.Empty(),
        ConvertedManaCost = Enumerable.Empty(),
        Name = Enumerable.Empty(),
        Power = Enumerable.Empty(),
        Text = Enumerable.Empty(),
        Toughness = Enumerable.Empty(),
        Types = Enumerable.Empty(),
        __document_id = o.__document_id
    })));

'System.Linq.Enumerable.Empty<TResult>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.由于显而易见的原因,这会爆炸。

有没有办法让这该死的东西工作?

我已经尝试过new string[0]new string[] { }而不是 Enumerable.Empty(),并且它们都失败了:

System.InvalidOperationException: Could not understand query:
-- line 2 col 219: invalid NewExpression
-- line 2 col 259: Can't parse double .0.0

  at Raven.Database.Linq.QueryParsingUtils.GetVariableDeclarationForLinqMethods(String query, Boolean requiresSelectNewAnonymousType) in c:\Builds\RavenDB-Stable\Raven.Database\Linq\QueryParsingUtils.cs:line 124
  at Raven.Database.Linq.DynamicViewCompiler.TransformMapDefinitionFromLinqMethodSyntax(String query, String& entityName) in c:\Builds\RavenDB-Stable\Raven.Database\Linq\DynamicViewCompiler.cs:line 355
  at Raven.Database.Linq.DynamicViewCompiler.HandleMapFunction(ConstructorDeclaration ctor, String map) in c:\Builds\RavenDB-Stable\Raven.Database\Linq\DynamicViewCompiler.cs:line 132
  at Raven.Database.Linq.DynamicViewCompiler.TransformQueryToClass() in c:\Builds\RavenDB-Stable\Raven.Database\Linq\DynamicViewCompiler.cs:line 97
  at Raven.Database.Linq.DynamicViewCompiler.GenerateInstance() in c:\Builds\RavenDB-Stable\Raven.Database\Linq\DynamicViewCompiler.cs:line 489
  at Raven.Database.DocumentDatabase.PutIndex(String name, IndexDefinition definition) in c:\Builds\RavenDB-Stable\Raven.Database\DocumentDatabase.cs:line 724
  at Raven.Database.Server.Responders.Index.Put(IHttpContext context, String index) in c:\Builds\RavenDB-Stable\Raven.Database\Server\Responders\Index.cs:line 71
  at Raven.Database.Server.Responders.Index.Respond(IHttpContext context) in c:\Builds\RavenDB-Stable\Raven.Database\Server\Responders\Index.cs:line 48
  at Raven.Database.Server.HttpServer.DispatchRequest(IHttpContext ctx) in c:\Builds\RavenDB-Stable\Raven.Database\Server\HttpServer.cs:line 548
  at Raven.Database.Server.HttpServer.HandleActualRequest(IHttpContext ctx) in c:\Builds\RavenDB-Stable\Raven.Database\Server\HttpServer.cs:line 315

我的 Reduce 看起来像这样:

this.Reduce = results => from r in results
                         group r by r.CardId into g
                         select new
                         {
                             CardId = g.Key,
                             Expansion = g.Select(r => r.Expansion).Where(v => v != null).FirstOrDefault(),
                             Owned = g.Sum(r => r.Owned),
                             Rarity = g.Select(r => r.Rarity).Where(v => v != null).FirstOrDefault(),
                             Artist = g.SelectMany(r => r.Artist),
                             Colors = g.SelectMany(r => r.Colors),
                             ConvertedManaCost = g.SelectMany(r => r.ConvertedManaCost),
                             Name = g.SelectMany(r => r.Name),
                             Power = g.SelectMany(r => r.Power),
                             Text = g.SelectMany(r => r.Text),
                             Toughness = g.SelectMany(r => r.Toughness),
                             Types = g.SelectMany(r => r.Types),
                         };

我的另一张地图是这样的:

this.AddMap<Card>(cards => from c in cards
                           select new
                           {
                               CardId = c.Id.ToString(),
                               Expansion = c.Expansion,
                               Owned = 0,
                               Rarity = c.Rarity,
                               Artist = c.NormalizedFaces.Select(f => f.Artist),
                               Colors = c.Colors,
                               ConvertedManaCost = c.NormalizedFaces.Select(f => f.ConvertedManaCost),
                               Name = c.NormalizedFaces.Select(f => f.Name),
                               Power = c.NormalizedFaces.Select(f => f.Power),
                               Text =
                                   c.NormalizedFaces.Select(f => f.Name)
                                   .Concat(c.NormalizedFaces.SelectMany(f => f.CardText))
                                   .Concat(c.NormalizedFaces.Select(f => f.Types))
                                   .Concat(c.NormalizedFaces.SelectMany(f => f.FlavorText)),
                               Toughness = c.NormalizedFaces.Select(f => f.Toughness),
                               Types = c.NormalizedFaces.Select(f => f.Types),
                           });

但是,我很确定这些不是罪魁祸首,因为索引仅适用于一张地图和减少。

4

2 回答 2

1

您只需要在地图功能中使用null而不是。Enumerable.Empty<T>()在你的 reduce 函数中,你可以这样做

Artist = g.Select(r => r.Artist).Where(x => x != null).FirstOrDefault()
于 2012-05-13T23:40:08.043 回答
0

替换Enumerable.Empty<string>()new string[0]

根据 Ayende 的说法,类型消失的问题不会得到解决。但是重写表达式以使用数组应该是诀窍。

于 2012-05-21T13:44:19.557 回答