RavenDB结果转换不';我不喜欢as关键字

RavenDB结果转换不';我不喜欢as关键字,ravendb,Ravendb,我正在使用下面的results transformer尝试从混合的文档集合中检索较少的数据。所有文档都从基类型继承,但我想从派生类型中提取一些字段(如果存在) 以下是我的尝试(为简洁起见,请删节): 但是,当我尝试将此结果转换器安装到服务器上时,我收到以下异常: System.ServiceModel.ServiceActivationException: The service '/MyService/MyService.svc' cannot be activated due to an e

我正在使用下面的results transformer尝试从混合的文档集合中检索较少的数据。所有文档都从基类型继承,但我想从派生类型中提取一些字段(如果存在)

以下是我的尝试(为简洁起见,请删节):

但是,当我尝试将此结果转换器安装到服务器上时,我收到以下异常:

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)]:错误-意外符号
As' [DomRegion FileName=,Begin=(9,33),End=(-1,-1)]:错误-意外符号
为'-->System.Net.WebException:远程服务器返回错误:(400)错误请求。
在System.Net.HttpWebRequest.GetResponse()中
位于Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func
1 getResponse) ---内部异常堆栈跟踪的结束--- 位于Raven.Client.Connection.HttpJsonRequest.HandleErrors(WebException e) 在Raven.Client.Connection.HttpJsonRequest.ReadJsonInternal(Func
1getresponse)
在Raven.Client.Connection.HttpJsonRequest.ReadResponseJson()上
位于Raven.Client.Connection.ServerClient.DirectPutTransformer(字符串名称、OperationMetadata操作元数据、TransformerDefinition定义)
在Raven.Client.Connection.ReplicationInformer.tryoOperation[T](Func
2操作,操作元数据操作元数据,操作元数据primaryOperationMetadata,布尔值避免抛出,T&result,布尔值&wasTimeout) 在Raven.Client.Connection.ReplicationInformer.ExecuteWithReplication[T](字符串方法,字符串primaryUrl,操作凭据primaryCredentials,Int32 currentRequest,Int32 currentReadStripingBase,Func
2操作)
在Raven.Client.Connection.ServerClient.ExecuteWithReplication[T](字符串方法,Func
2操作) 位于Raven.Client.index.AbstractTransformerCreationTask.Execute(IDatabaseCommands databaseCommands,DocumentConvention) 在Raven.Client.index.IndexCreation.CreateIndexes(ExportProvider CatalogToGetNindexingTaskFrom、IDocumentStore documentStore) 在c:\Users\me\repos\Admin\MyService\MyService.svc.cs中的MyService.MyService..ctor()处:第51行 ---内部异常堆栈跟踪的结束--- //更多堆栈跟踪(可能不需要)

看来我投错了。获取这些派生类属性的最佳方法是什么?还是我在尝试不可能的事情


非常感谢您的帮助。

您正在尝试使用服务器上不存在的类型。
您可以通过在客户端的实例上调用
AsDocument(doc)
来获得动态行为。

我不明白尝试使用AsDocument意味着什么,我尝试使用它导致了丑陋的代码无法工作,所以我放弃了,并尝试想出另一个想法

事实证明,AbstractTransformerCreationTask中的文档类型不必与索引中的文档类型对齐,它只是让您更容易进行intellisense

因此,为了解决这个问题,我让我的transformer类使用SearchResult类型作为返回的类型

public class DocumentAsSearchResultTransformer : AbstractTransformerCreationTask<SearchResult>
{
    ...
}
太棒了

然后我遇到了一个问题,虽然我不想让一些更大的属性回来,但我想计算它们的一些值。谢天谢地,这很容易解决。如果在SearchResult模型中包含不希望从搜索中返回的属性,则可以在转换中使用它们,即使您没有从转换中返回它们

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的Votes属性保留为空,因此它不会从查询中返回。但是你的计算属性会

从服务器端的转换来看,这也是一个非常简洁的转换


希望这能帮助其他人处理类似情况。

最初的想法是尝试使用动态类型。但是没有骰子,因为它抛出了这个错误:“表达式树可能不包含动态操作”服务器上的派生类型中有文档,但不在我为该类输入的type参数中的基类型中。我试试看。这是否仍会导致服务器上出现类似的转换?主要考虑性能方面的原因。当你在客户端说,你的意思是根本不使用转换吗?或者你的意思是在定义变换时?
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
        ..
    }
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
    }