在this处遵循本教程时,所有Gremlin Query示例都会返回动态结果。
像:
IDocumentQuery<dynamic> query = client.CreateGremlinQuery<dynamic>(graph, "g.V().hasLabel('person')");
dynamic result in await query.ExecuteNextAsync();
结果如下:
{"id":"thomas","label":"person","type":"vertex","properties":{"firstName":[{"id":"8fec3d0f-7290-4e80-9f38-9306de998579","value":"Thomas"}],"age":[{"id":"932879da-55c0-4dbc-b68c-fb3ef1e1827a","value":44}]}}
但是如何使用CreateGremlinQuery
和ExecuteNextAsync
来返回普通班级Person
?
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
答案 0 :(得分:0)
我的解决方案就像:
1] 注释人员类:
public class Person
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("firstName")]
public string FirstName { get; set; }
[JsonProperty("lastName")]
public string LastName { get; set; }
[JsonProperty("age")]
public int Age { get; set; }
}
2] 将结果转换为JObject并循环属性:
IDocumentQuery<dynamic> query = client.CreateGremlinQuery<dynamic>(graph, "g.V().hasLabel('person')");
while (query.HasMoreResults)
{
foreach (JObject item in await query.ExecuteNextAsync<dynamic>())
{
var properties = (JObject)item["properties"];
var personAsJObject = new JObject();
foreach (var property in properties)
{
if (property.Value is JArray valueArray)
{
personAsJObject.Add(property.Key, valueArray.First["value"]);
}
}
var person = personAsJObject.ToObject<Person>(); // Real person here
}
}