当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - RavenDB is Screwing up my Multi Map query. What am I doing wrong?

问题描述:

The long and short of it is that I am trying to add this bit to a Raven Multi Map query:

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>(),

});

And Raven converts that expression into this (formatted for readability):

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

})));

Which blows up with 'System.Linq.Enumerable.Empty<TResult>()' cannot be inferred from the usage. Try specifying the type arguments explicitly. for obvious reasons.

Is there any way to get this damn thing working?

I've tried new string[0] and new string[] { } instead of Enumerable.Empty(), and those both fail with:

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 looks like this:

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),

};

And my other map looks like this:

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),

});

However, I'm pretty sure those are not the culprit, because the index WORKS with just the one map and reduce.

网友答案:

You need to use just null instead of Enumerable.Empty<T>() in your map function. In your reduce function, you can do it like this

Artist = g.Select(r => r.Artist).Where(x => x != null).FirstOrDefault()
网友答案:

Replace Enumerable.Empty<string>() with new string[0].

According to Ayende, the issue of the types disappearing won't be fixed. But rewriting the expression to use an array should to the trick.

分享给朋友:
您可能感兴趣的文章:
随机阅读: