3

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

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 中......仍然失败。

4

1 回答 1

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,
                                };
    }
}
于 2012-05-24T09:58:15.360 回答