【问题标题】:Microsoft.Azure.Graphs : how to cast the result to a type?Microsoft.Azure.Graphs:如何将结果转换为类型?
【发布时间】:2018-05-02 19:03:19
【问题描述】:

tutorial-develop-graph-dotnet 学习本教程时,所有 Gremlin 查询示例都返回动态结果。

喜欢:

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}]}}

但是如何使用CreateGremlinQueryExecuteNextAsync 来返回一个普通的类Person 呢?

public class Person
{
    public string Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }
}

【问题讨论】:

    标签: .net azure facebook-graph-api gremlin


    【解决方案1】:

    我的解决方案是这样的:

    1] 注释 person 类:

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多