ravendb - 查询索引时引发异常

标签 ravendb

我有一个看起来像这样的索引

public class FeedAnnouncementByOneLabel : Raven.Client.Indexes.AbstractIndexCreationTask<FeedPost, FeedAnnouncementByOneLabel.Result>
{
    public class Result
    {
        public long SequentialId { get; set; }
        public string AnnouncementId { get; set; }
        public string Label1 { get; set; }
        public string FeedOwner { get; set; }
        public DateTimeOffset CreationDate { get; set; }
    }
    public FeedAnnouncementByOneLabel()
    {
        Map = announcements => from doc in announcements
                               from docLabelsItem1 in ((IEnumerable<Label>)doc.Labels).DefaultIfEmpty()
                               select new Result
                               {
                                   SequentialId = doc.SequentialId,
                                   AnnouncementId = doc.AnnouncementId,
                                   CreationDate = doc.CreationDate,
                                   FeedOwner = doc.FeedOwner,
                                   Label1 = docLabelsItem1.Text
                               };
    }
}

我这样查询(简化版本,仍然失败):
from c in _session.Query<FeedAnnouncementByOneLabel.Result, FeedAnnouncementByOneLabel>()
                                   select c;

我每次查询都会收到异常。真正奇怪的是,这曾经奏效。我不确定自从我将Raven更新到最新版本以来,它是否坏了-或由于其他原因我改变了。
我可以肯定,唯一的改变就是我将“FeedPost”移入了自己的DLL(带有各种DataContract属性)。

有没有人?

谢谢
<Exception xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ExceptionType>System.InvalidCastException</ExceptionType>
<Message>
Unable to cast object of type 'FeedPost' to type 'Result'.
</Message>
<StackTrace>
at Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity[T](String id, RavenJObject documentFound, RavenJObject metadata) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:line 416
 at Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity[T](String key, RavenJObject document, RavenJObject metadata) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:line 340
 at Raven.Client.Document.SessionOperations.QueryOperation.Deserialize[T](RavenJObject result) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:line 130
 at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
 at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
 at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
 at Raven.Client.Document.SessionOperations.QueryOperation.Complete[T]() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:line 114
 at Raven.Client.Document.AbstractDocumentQuery`2.GetEnumerator() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\AbstractDocumentQuery.cs:line 603
 at Raven.Client.Linq.RavenQueryInspector`1.GetEnumerator() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryInspector.cs:line 98
 at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
 at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
 at FeedPostsController.Get(String labels, Int32 sincePostId) in FeedPostsController.cs:line 211
 at lambda_method(Closure , Object , Object[] )
 at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
 at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.Execute(HttpControllerContext controllerContext, IDictionary`2 arguments)
 at System.Web.Http.Controllers.ApiControllerActionInvoker.<>c__DisplayClass2.<InvokeActionAsync>b__0()
 at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
</StackTrace>
</Exception>

[更新]

好的-我将FeedPost定义移回了具有索引的DLL中……仍然失败。

最佳答案

刚刚尝试了这个(https://gist.github.com/2780374),但我没有得到该错误(即查询运行没有错误),但没有结果

似乎您正在尝试通过标签获取所有供稿,那么这可能行得通吗?
我删除了annoucementId和其他ID,因为它会妨碍聚合(按标签),但是也许我弄错了您的域。

    public class FeedAnnouncementByOneLabel : AbstractIndexCreationTask<FeedPost, FeedAnnouncementByOneLabel.Result>
{
    public class Result
    {
        public string Label1 { get; set; }
        public string FeedOwner { get; set; }
        public DateTimeOffset CreationDate { get; set; }
    }
    public FeedAnnouncementByOneLabel()
    {
        Map = announcements => from doc in announcements
                               from label in doc.Labels.DefaultIfEmpty()
                               select new 
                               {
                                   CreationDate = (DateTimeOffset)doc.CreationDate,
                                   FeedOwner = doc.FeedOwner,
                                   Label1 = label.Text
                               };

        Reduce = results => from result in results
                            group result by result.Label1
                                into r
                                select new
                                {
                                    CreationDate = (DateTimeOffset)r.Max(x => x.CreationDate),
                                    FeedOwner = r.Select(x=> x.FeedOwner).First(),
                                    Label1 = r.Key,
                                };
    }
}

关于ravendb - 查询索引时引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732088/

相关文章:

ravendb - 我需要怎么做才能在RavenDB中存储对象?

c# - 用于金融时间序列数据的 NoSql(例如 RavenDB)?

c# - RavenDB ID 前缀和 REST API

linq - RavenDB OrderByDescending 和 Take - 错误结果

database - 如果我没有任何文档,我无法在 ravendb 中创建一个集合

c# - 如何从 RavenDB 数据库中获取所有实体名称(集合名称)?

c# - 如何在 RavenDB 中的对象集合中搜索 "Not Contains"条件

RavenDb和MultiTenancy

mongodb - 使用 Docker 容器设计系统架构

ravendb - RavenDB:如何使用多个搜索词进行查询