查询索引时抛出异常

时间:2012-05-24 06:13:21

标签: 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
                               };
    }
}

我这样查询(简化版,STILL失败):

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>

[UPDATE]

好的 - 我将FeedPost定义移回到具有索引的同一个DLL中......仍然失败。

1 个答案:

答案 0 :(得分:1)

刚试过这个(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,
                                };
    }
}
相关问题