将变量传递给 SparqlParameterizedString dotNetRDF

passing a variable to SparqlParameterizedString dotNetRDF

我正在尝试将参数化查询传递给 dotNetRDF 中的 ExecuteQery() 函数

我的密码是

   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 );

我无法在 SetVariable 函数的第二个参数中设置变量 preference,因为它说它是一个无效参数。我在文档中读到说参数必须是 INode 值 我试图使用

获取变量 preference 的 INode 值
INode value = preference.Value("var");

但无法完成,因为它显示错误“String does not contain a definition for value"

谁能帮我得到这个字符串变量的 INode 值或者 如何正确调用此 SetVariable 方法

首先,您的查询模板看起来很糟糕,您有 my:@variable 这只会导致无效查询,无论您注入什么值。 @variable 也是一个参数,必须通过 SetParameter()SetUri()SetLiteral() 方法之一注入。

看起来您实际想要注入的是 URI <http://www.codeproject.com/KB/recipes/n3_notation#foo>,其中 foo 是您的 d1.send() 方法返回的字符串。并且您正在尝试使用 PREFIX 声明将其压缩为 my:foo

因此,如果您想注入 URI,则可以直接使用 SetUri(),例如

// 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));