RavenDB正在搞砸我的Multi Map查询。我究竟做错了什么?

时间:2012-05-13 22:46:20

标签: c# ravendb

它的长短是我试图将这个位添加到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

My 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),
                           });

但是,我很确定那些不是罪魁祸首,因为索引WORKS只有一个map而且还原。

2 个答案:

答案 0 :(得分:1)

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

Artist = g.Select(r => r.Artist).Where(x => x != null).FirstOrDefault()

答案 1 :(得分:0)

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

根据Ayende的说法,类型消失的问题将无法解决。但是重写表达式以使用数组应该是技巧。