RavenDB:反序列化嵌套数组

RavenDB: Deserializing nested arrays

假设我有以下 class 要序列化并存储为 RavenDB 的文档:

public class Block
{
    public string Id { get; set; }
    public List<List<dynamic>> data { get; set; }
}

存储后,在数据库中可以看到这样一个文档:

{
"Id": "f539770a",
"columns": [ 
              [ 90, 91, 92, 93, 94 ], 
              [ "C", "D", "A", "B", "C" ] 
           ] }

我想执行查询以检索 "Columns" 字段中的第 N 个值列表:

session.Query<Block>().Where(b => b.Id == "f539770a").Select(b =>b.columns[i]);

我收到以下错误:

{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath '__document_id'."}

似乎查询正常(服务器 returns http 200),但存在客户端反序列化问题。

我是不是漏掉了什么?

谢谢!!

更新:

我更改了数据结构,因为错误似乎提示:

public class Column
{
    public List<dynamic> data { get; set; }
}

public class Block
{
    public string Id { get; set; }
    public List<Column> columns { get; set; }
}

存储的文档是这样的:

{
"Id": "f539770a",
"columns": [
             { "data": [ 95, 96, 97, 98, 99 ] },
             { "data": [ "D", "A", "B", "C", "D" ] }
           ]}

执行此查询后:

session.Query<Block>().Include<Block>(b => b.columns).Where(b => b.parentFileId == dbFileDescriptor.Id).Select(b => b.columns[i])

我没有异常,但没有加载嵌套数组:

删除 Include。这不是它的用途。 Include 用于预加载对其他文档的引用,因此您不必进行多次数据库访问。您可能会让 RavenDB 查找 Block's 及其对应的 Column 文档。请参阅文档 here.

session.Query<Block>().Where(b => b.parentFileId == dbFileDescriptor.Id).Select(b => b.data[i]);

编辑:

如果您要按 ID 查找内容,请使用 Load 而不是 Query

session.Load<Block>("f539770a").data[i];

我验证了这适用于使用 RavenDB.Tests.Helpers nuget 包和 Shouldly 断言的 raven 单元测试。

    public class SoQuestion : RavenTestBase
    {
        [Fact]
        public void GetBlock_Success()
        {
            using (var docStore = NewDocumentStore())
            {
                using (var session = docStore.OpenSession())
                {
                    session.Store(new Block
                    {
                        Id = "f539770a",
                        data = new List<List<dynamic>>()
                        {
                            new List<dynamic>()
                            {
                                90,
                                91,
                                92,
                                93,
                                94
                            },
                            new List<dynamic>()
                            {
                                "C",
                                "D",
                                "A",
                                "B",
                                "C"
                            }
                        }
                    });
                    session.SaveChanges();
                }

                docStore.WaitForStaleIndexesToComplete();

                //Act
                var block = GetBlock(docStore);

                //Assert
                block.ShouldNotBe(null);
                block.data.ShouldNotBeEmpty();
            }
        }

        private Block GetBlock(IDocumentStore documentStore)
        {
            using (var session = documentStore.OpenSession())
            {
                return session.Load<Block>("f539770a");
            }
        }

        public class Block
        {
            public string Id { get; set; }
            public List<List<dynamic>> data { get; set; }
        }
    }