Search code examples
listpropertiesneo4jneo4jclient

neo4jClien Create node with paramaters using a List<Properties>


I am trying to create a node that takes a list of properties and uses the list object to create those properties. Is there any way to do this?

public List<PropertiesModel> node_properties;

public class PropertiesModel{ 
    public string propertyName { get; set; }
    public string propertyValue { get; set; }
}

then when I pass this on to:

client.Cypher
.Create("(n:Label {node})")
         .WithParam("node", node_properties)
                .ExecuteWithoutResults(); 

I get the following error when I run this:

CypherTypeException: Collections containing mixed types can not be stored in properties.

I am guessing since this is a list, containing a model made of strings it does not like it. Is there another way to go about building dynamic models of paramaters? I thought about IDictionary but it seems I may have issues mapping directly from a JSON post into a IDictionary.

Thanks


Solution

  • ok, so I am feeling slow. Just answered my own question.

    I had

    class nodeModel{ 
    List<PropertiesModel> props {get; set;}
    }
    class PropertiesModel{    
       public string propertyName { get; set; }
       public string propertyValue { get; set; }
    }
    

    so I needed to do:

    client.Cypher
    .Create("(n:Label {node})")
         .WithParam("node", node_properties.props)
                .ExecuteWithoutResults(); 
    

    I had to step into the list basically with the .props

    but this was not giving me my intended results. Say my list contained 2 properties that I was intending to have attached to 1 node. This actually created a node per property. To solve this, I mapped this over to a IDictionary.

    IDictionary<string, string> map = node_properties.props.ToDictionary(p=>p.propertyname, p=>p.propertyValue);
    

    then I changed the .WithParams part of the query statement to:

    .WithParams(map)
    

    And The intended result was 1 node made up of a list of properties.