首页 > 解决方案 > 如何使用 Neo4jClient 将 C# DateTime 存储为 Neo4j DATE_TIME 而不是 STRING?

问题描述

我正在尝试将 C# DateTime 属性存储为本机 Neo4j DATE_TIME,但它们始终存储为STRING

简化示例代码:

// properties
IDictionary<string, dynamic> parameterMapCreate = new Dictionary<string, dynamic>();
parameterMapCreate.Add("createdAt", Datetime.UtcNow);

// labels
var labelsToAdd = "Label01:Label02";

// query
var cypherQuery = _graphClient.Cypher
    .WithParams(new
    {
        tok = Guid.NewGuid(),
        propertiesCreate = parameterMapCreate
    })
    .Merge($"(n:{"TestNodeLabel"} {{ {"token"}: $tok }})")
    .OnCreate().Set("n = $propertiesCreate")
    .Set("n:" + labelsToAdd)
    .With("n, properties(n) as prop")
    .ReturnDistinct((n, prop) => new GenericNode
    {
        Id = n.Id(),
        Labels = n.Labels(),
        Properties = prop.As<Dictionary<string, string>>(),
    });
    
// generic node (doesn't affect storage type, only what we get back)
public class GenericNode
{
    public long Id { get; set; }
    public IEnumerable<string> Labels { get; set; }
    public Dictionary<string, string> Properties { get; set; }
}

数字和布尔类型存储正确,但日期存储为STRING,如以下查询所示:

MATCH (n)
WITH (apoc.meta.cypher.types(n)) as types
return types

给出:

types
{
  "createdAt": "STRING",
  "token": "STRING"
}

我看到使用自定义 POCO 我们可以用属性装饰[Neo4jDateTime]属性,但是如果我们没有固定的类/属性并且只想通过字典传递一堆属性,如图所示?

谢谢,理查德

标签: c#neo4jneo4jclient

解决方案


推荐阅读