获取实体名称和关联属性的列表

时间:2014-06-27 00:19:54

标签: json ravendb

我在我的数据库中动态创建了一些集合。我实例化ExpandoObjects并手动设置实体名称。这很好。

现在我需要获取一个实体列表,其中包含相关属性( 对于集合中的所有文档都是相同的,但不是必然)< / p>

结果应该是以下列表:

class Collection
{
    string Name;
    string[] Properties;
}

现在,我知道我可以创建一个引用["@metadata"]["Raven-Entity-Name"]的索引,由于JSON对象(或RavenJObjectsExpandoObjects)是字典,我以为我能够检索doc.Keys (我认为这不起作用)

这是我正在进行的工作,但不起作用(Properties数组在集合中每个文档包含一个null

地图:

from doc in docs
let Name = doc["@metadata"]["Raven-Entity-Name"]
where Name.StartsWith("External_")
select new { Name, Properties = doc.Keys }

减少

from result in results
group result by result.Name into r
select new { Name = r.Key, Properties = r.Select(x => x.Properties).Distinct() }

1 个答案:

答案 0 :(得分:1)

嗯,我走在正确的轨道上。我终于明白了。

地图:

from doc in docs
let Name = doc["@metadata"]["Raven-Entity-Name"]
where Name.StartsWith("External_")
from property in doc
select new { Name, Properties = new[] { property.Key } }

减少

from result in results
group result by result.Name into r
select new { Name = r.Key, Properties = r.SelectMany(x => x.Properties).Distinct() }