Search code examples
c#inodedotnetrdf

passing a variable to SparqlParameterizedString dotNetRDF


im trying to pass a parameterized query to to ExecuteQery() function in dotNetRDF

my code is

   preference =  (d1.send()); // d1.send(); method returns a string value


   SparqlParameterizedString queryString = new SparqlParameterizedString();
   queryString.CommandText = @"
   PREFIX my: <http://www.codeproject.com/KB/recipes/n3_notation#>
   SELECT ?name WHERE { [ a my:spec; my:preferedby my:@variable;  my:name ?name].  }";

   queryString.SetVariable("variable", preference );

I cant set the variable preference in the second parameter in the SetVariable function since it says its an invalid argument. i read in the document saying that the parameter must be a INode value i tried to get the INode value of variable preference using

INode value = preference.Value("var");

but it cannot be done since it shows an error "String does not contain a definition for value"

can some one please help me to get the INode value of this String variable or how to call this SetVariable Method correctly


Solution

  • For a start your query template looks bad, you have my:@variable which is only going to result in in an invalid query whatever value you inject. Also @variable is a parameter and must be injected via one of the SetParameter(), SetUri() or SetLiteral() methods.

    It looks like what you actually want to inject is the URI <http://www.codeproject.com/KB/recipes/n3_notation#foo> where foo is the string returned by your d1.send() method. And that you are trying to compact this to my:foo using the PREFIX declaration.

    So if you want to inject a URI then you can instead use the SetUri() directly e.g.

    // Start a new query string
    SparqlParameterizedString queryString = new SparqlParameterizedString();
    
    // Set the desired prefix declarations
    queryString.Namespaces.AddNamespace("my", new Uri("http://www.codeproject.com/KB/recipes/n3_notation#"));
    
    // Set the command template
    queryString.CommandText = @"SELECT ?name WHERE 
    { 
      [ a my:spec ; 
        my:preferedby @variable ;  
        my:name ?name ].  
    }";
    
    // Set your parameter
    queryString.SetUri("variable", new Uri("http://www.codeproject.com/KB/recipes/n3_notation#" + preference));