C# 将变量传递给SparqlParameterizedString dotNetRDF
我正在尝试将参数化查询传递给dotNetRDF中的ExecuteQery()函数 我的代码是C# 将变量传递给SparqlParameterizedString dotNetRDF,c#,inode,dotnetrdf,C#,Inode,Dotnetrdf,我正在尝试将参数化查询传递给dotNetRDF中的ExecuteQery()函数 我的代码是 preference = (d1.send()); // d1.send(); method returns a string value SparqlParameterizedString queryString = new SparqlParameterizedString(); queryString.CommandText = @" PREFIX my: <h
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 );
但由于显示错误“字符串不包含值定义”
有人能帮我得到这个字符串变量的INode值吗
如何正确调用此
SetVariable
方法作为一个开始,您的查询模板看起来很糟糕,您有my:@variable
,这只会导致无效的查询,无论您输入什么值。另外,@变量
是一个参数,必须通过SetParameter()
、SetUri()
或SetLiteral()
方法之一进行注入
看起来您实际上想要注入的是URI
,其中foo
是d1.send()方法返回的字符串。您正试图使用前缀
声明将其压缩为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));
// 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));