RavenDB 结果转换不喜欢 as 关键字
RavenDB Results Transform doesn't like the as keyword
我正在使用以下结果转换器尝试从混合文档集合中检索较少的数据。所有文档都继承自基本类型,但我想从派生类型中提取一些字段(如果存在)。
这是我的尝试(为简洁起见删减):
public DocumentAsSearchResultTransformer()
{
TransformResults = docs => from doc in docs
// need several casts to get the properties of the lower cast variables
let subDoc = doc as SubDocumentType
let subDoc2 = doc as SubDocumentType2
select new SearchResult
{
EntityId = doc.EntityId,
Name = doc.Name
// start pulling out document type specific fields
Categories = subDoc != null ? subDocCategories : null,
UserTags = subDoc2 != null ? subDoc2.UserTags : null
};
}
但是,当我尝试将此结果转换器放到服务器上时,我收到以下异常:
System.ServiceModel.ServiceActivationException: The service '/MyService/MyService.svc' cannot be activated due to an exception during compilation. The exception message is: Exception has been thrown by the target of an invocation..
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Raven.Abstractions.Exceptions.BadRequestException: Could not understand query: Could not understand query:
[DomRegion FileName=, Begin=(3, 18), End=(-1, -1)]: 错误 - 意外符号 As'
[DomRegion FileName=, Begin=(6, 27), End=(-1, -1)]: Error - Unexpected symbol
As'
[DomRegion FileName=, Begin=(9, 33), End=(-1, -1)]: 错误 - 意外符号 As' ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func
1 getResponse)
--- 内部异常堆栈跟踪结束 ---
在 Raven.Client.Connection.HttpJsonRequest.HandleErrors(WebException e)
在 Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func1 getResponse)
at Raven.Client.Connection.HttpJsonRequest.ReadResponseJson()
at Raven.Client.Connection.ServerClient.DirectPutTransformer(String name, OperationMetadata operationMetadata, TransformerDefinition definition)
at Raven.Client.Connection.ReplicationInformer.TryOperation[T](Func
2 操作,OperationMetadata operationMetadata,OperationMetadata primaryOperationMetadata,Boolean avoidThrowing,T& 结果,Boolean& wasTimeout)
在 Raven.Client.Connection.ReplicationInformer.ExecuteWithReplication[T](字符串方法、字符串 primaryUrl、OperationCredentials primaryCredentials、Int32 currentRequest、Int32 currentReadStripingBase、Func2 operation)
at Raven.Client.Connection.ServerClient.ExecuteWithReplication[T](String method, Func
2 操作)
在 Raven.Client.Indexes.AbstractTransformerCreationTask.Execute(IDatabaseCommands 数据库命令,DocumentConvention documentConvention)
在 Raven.Client.Indexes.IndexCreation.CreateIndexes(ExportProvider catalogToGetnIndexingTasksFrom,IDocumentStore documentStore)
在 MyService.MyService..ctor() 中 c:\Users\me\repos\Admin\MyService\MyService.svc.cs: 第 51 行
--- 内部异常堆栈跟踪结束 ---
// 更多堆栈跟踪(可能不需要)
看来我选错了。我获取这些派生的 class 属性的最佳方式是什么?还是我在尝试不可能的事情?
感谢任何帮助。
您正在尝试使用服务器上不存在的类型。
您可以通过在客户端实例上调用 AsDocument(doc)
来获得动态行为。
我不明白尝试使用 AsDocument 是什么意思,我尝试使用它的结果是丑陋的代码无法正常工作,所以我放弃了它并尝试提出另一个想法。
事实证明,AbstractT运行sformerCreationTask 中的 不必与索引中的文档类型对齐,它只是让您更轻松地使用智能感知。
所以,为了解决这个问题,我让我的 t运行sformer class 使用 SearchResult 类型作为 ,同时也是我返回的类型。
public class DocumentAsSearchResultTransformer : AbstractTransformerCreationTask<SearchResult>
{
...
}
然后我就可以正常分配文档中的属性了。
TransformResults = docs => (from doc in docs
select new SearchResult
{
Name = doc.Name,
Categories = doc.Categories, // this is from a derived document type really, but if the doc doesn't have it it'll be null
..
}
效果很好!
然后我 运行 进入了这个问题,尽管我不希望返回一些更大的属性,但我想从中计算出一些值。谢天谢地,这很容易解决。如果您在 SearchResult 模型中包含了您不希望从搜索中返回的那些属性,那么您可以在 t运行s 表单中使用它们,即使您不将它们从 t运行变形。
TransformResults = docs => (from doc in docs
select new SearchResult
{
Name = doc.Name,
Categories = doc.Categories,
// pretend that doc.Votes is a collection of large objects that you don't want coming back from a search
UpvotesCount = doc.Votes != null ? doc.Votes.Count(v => v.Type == "Upvote") : 0
}
由于您已将所选 SearchResult 的投票 属性 留空,因此它不会从查询中返回。但是你的计算 属性 会。
看看它在服务器端由此生成的 t运行sform,它也是一个非常简洁的 t运行sform。
希望这对处理类似情况的其他人有所帮助。
我正在使用以下结果转换器尝试从混合文档集合中检索较少的数据。所有文档都继承自基本类型,但我想从派生类型中提取一些字段(如果存在)。
这是我的尝试(为简洁起见删减):
public DocumentAsSearchResultTransformer()
{
TransformResults = docs => from doc in docs
// need several casts to get the properties of the lower cast variables
let subDoc = doc as SubDocumentType
let subDoc2 = doc as SubDocumentType2
select new SearchResult
{
EntityId = doc.EntityId,
Name = doc.Name
// start pulling out document type specific fields
Categories = subDoc != null ? subDocCategories : null,
UserTags = subDoc2 != null ? subDoc2.UserTags : null
};
}
但是,当我尝试将此结果转换器放到服务器上时,我收到以下异常:
System.ServiceModel.ServiceActivationException: The service '/MyService/MyService.svc' cannot be activated due to an exception during compilation. The exception message is: Exception has been thrown by the target of an invocation..
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Raven.Abstractions.Exceptions.BadRequestException: Could not understand query: Could not understand query:
[DomRegion FileName=, Begin=(3, 18), End=(-1, -1)]: 错误 - 意外符号 As'
[DomRegion FileName=, Begin=(6, 27), End=(-1, -1)]: Error - Unexpected symbol
As'
[DomRegion FileName=, Begin=(9, 33), End=(-1, -1)]: 错误 - 意外符号 As' ---> System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func
1 getResponse)
--- 内部异常堆栈跟踪结束 ---
在 Raven.Client.Connection.HttpJsonRequest.HandleErrors(WebException e)
在 Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func1 getResponse)
at Raven.Client.Connection.HttpJsonRequest.ReadResponseJson()
at Raven.Client.Connection.ServerClient.DirectPutTransformer(String name, OperationMetadata operationMetadata, TransformerDefinition definition)
at Raven.Client.Connection.ReplicationInformer.TryOperation[T](Func
2 操作,OperationMetadata operationMetadata,OperationMetadata primaryOperationMetadata,Boolean avoidThrowing,T& 结果,Boolean& wasTimeout)
在 Raven.Client.Connection.ReplicationInformer.ExecuteWithReplication[T](字符串方法、字符串 primaryUrl、OperationCredentials primaryCredentials、Int32 currentRequest、Int32 currentReadStripingBase、Func2 operation)
at Raven.Client.Connection.ServerClient.ExecuteWithReplication[T](String method, Func
2 操作)
在 Raven.Client.Indexes.AbstractTransformerCreationTask.Execute(IDatabaseCommands 数据库命令,DocumentConvention documentConvention)
在 Raven.Client.Indexes.IndexCreation.CreateIndexes(ExportProvider catalogToGetnIndexingTasksFrom,IDocumentStore documentStore)
在 MyService.MyService..ctor() 中 c:\Users\me\repos\Admin\MyService\MyService.svc.cs: 第 51 行
--- 内部异常堆栈跟踪结束 ---
// 更多堆栈跟踪(可能不需要)
看来我选错了。我获取这些派生的 class 属性的最佳方式是什么?还是我在尝试不可能的事情?
感谢任何帮助。
您正在尝试使用服务器上不存在的类型。
您可以通过在客户端实例上调用 AsDocument(doc)
来获得动态行为。
我不明白尝试使用 AsDocument 是什么意思,我尝试使用它的结果是丑陋的代码无法正常工作,所以我放弃了它并尝试提出另一个想法。
事实证明,AbstractT运行sformerCreationTask 中的
所以,为了解决这个问题,我让我的 t运行sformer class 使用 SearchResult 类型作为
public class DocumentAsSearchResultTransformer : AbstractTransformerCreationTask<SearchResult>
{
...
}
然后我就可以正常分配文档中的属性了。
TransformResults = docs => (from doc in docs
select new SearchResult
{
Name = doc.Name,
Categories = doc.Categories, // this is from a derived document type really, but if the doc doesn't have it it'll be null
..
}
效果很好!
然后我 运行 进入了这个问题,尽管我不希望返回一些更大的属性,但我想从中计算出一些值。谢天谢地,这很容易解决。如果您在 SearchResult 模型中包含了您不希望从搜索中返回的那些属性,那么您可以在 t运行s 表单中使用它们,即使您不将它们从 t运行变形。
TransformResults = docs => (from doc in docs
select new SearchResult
{
Name = doc.Name,
Categories = doc.Categories,
// pretend that doc.Votes is a collection of large objects that you don't want coming back from a search
UpvotesCount = doc.Votes != null ? doc.Votes.Count(v => v.Type == "Upvote") : 0
}
由于您已将所选 SearchResult 的投票 属性 留空,因此它不会从查询中返回。但是你的计算 属性 会。
看看它在服务器端由此生成的 t运行sform,它也是一个非常简洁的 t运行sform。
希望这对处理类似情况的其他人有所帮助。