C# 如何在项目中使用HasPropertyValueSelector?

C# 如何在项目中使用HasPropertyValueSelector?,c#,.net,parsing,dotnetrdf,C#,.net,Parsing,Dotnetrdf,我是dotNetRDF的新手,我遇到了一个问题,可能是名称空间或框架 虽然我正在将名称空间添加到my项目中,但以下代码不起作用 系统给定了一个找不到命名空间的错误 HasPropertyValueSelector sel = new HasPropertyValueSelector(rdfType, carnivore); 所有代码如下 using System; using System.Collections.Generic; using VDS.RDF; using VDS.RDF.Pa

我是dotNetRDF的新手,我遇到了一个问题,可能是名称空间或框架

虽然我正在将名称空间添加到my项目中,但以下代码不起作用

系统给定了一个找不到命名空间的错误

HasPropertyValueSelector sel = new HasPropertyValueSelector(rdfType, carnivore);
所有代码如下

using System;
using System.Collections.Generic;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Query;
        static void Main(string[] args)
        {
            Graph g = new Graph();
            UriLoader.Load(g, new Uri("http://example.org/animals"));
            IUriNode rdfType = g.CreateUriNode("rdf:type");
            IUriNode carnivore = g.CreateUriNode("ex:Carnivore");

           ***HasPropertyValueSelector sel = new HasPropertyValueSelector(rdfType, carnivore);***
           IEnumerable<Triple> carnivores = g.GetTriples(sel);

            Graph ourlist = new Graph();
            ourlist.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/"));

            IUriNode rdfType2 = ourlist.CreateUriNode("rdf:type");
            IUriNode animal = ourlist.CreateUriNode("ex:Animal");

            foreach (Triple t in carnivores)
            {

               ourlist.Assert(new Triple(Tools.CopyNode(t.Subject, ourlist), rdfType2, animal));
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用VDS.RDF;
使用VDS.RDF.Parsing;
使用VDS.RDF.Query;
静态void Main(字符串[]参数)
{
图g=新图();
加载(g,新Uri(“http://example.org/animals"));
IUriNode rdfType=g.CreateUriNode(“rdf:type”);
IUriNode肉食动物=g.CreateUriNode(“ex:肉食动物”);
***HasPropertyValueSelector sel=新的HasPropertyValueSelector(rdfType,肉食动物)***
IEnumerable食肉动物=g.GetTriples(sel);
Graph ourlist=新图形();
ourlist.NamespaceMap.AddNamespace(“ex”),新Uri(“http://example.org/"));
IUriNode rdfType2=ourlist.CreateUriNode(“rdf:type”);
IUriNode-animal=ourlist.CreateUriNode(“ex:animal”);
foreach(食肉动物的三重t)
{
Assert(新的三元组(Tools.CopyNode(t.Subject,ourlist),rdfType2,animal));
}
}
}
}

您缺少的一个步骤是在创建使用名称空间的URI节点之前定义名称空间。这是通过IGraph接口上的NamespaceMapper完成的。您可以阅读更多关于它的内容,但一个简单的例子是:

IGraph g=新图形();
//定义要使用的名称空间
g、 AddNamespace(“rdf”),新的Uri(“http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
g、 AddNamespace(“ex”),新Uri(“http://example.org/"));
//定义与上一示例相同的三元组
UriNode rdfType=g.CreateUriNode(“rdf:type”);
UriNode exCarnivore=g.CreateUriNode(“ex:肉食动物”);
var肉食动物=g.GetTriplesWithPredicateObject(RDF类型,exCarnivore);

如果你避免像“系统无法识别此声明”这样的含糊其辞的陈述,那么你作为工程师的生活就会轻松得多。没有人知道这意味着什么。使用目标语句,如“此代码行引发编译错误”类型或未找到命名空间;是否缺少程序集引用?”或“程序到达此行时引发异常。异常为“未找到对象引用”“诸如此类。请讲一下技术细节。欢迎来到SO顺便说一句,你是对的。感谢您的建议。NB-您还需要添加详细信息,例如您正在使用的库的版本。这看起来像是一个非常古老的API,很久以前就被弃用/从库中删除了。另外,这不是一个有效的C代码,你没有跳过整个过程吗?您似乎缺少代码中的
名称空间YourNamespace
类yournclass
定义谢谢您的回答。我查看您给定的文档并理解它并定义名称空间,但系统仍然给出了一个未找到名称空间的错误。HasPropertyValueSelector sel=新的HasPropertyValueSelector(rdfType,肉食动物);我无法从dotNetRDF代码库中识别
HasPropertyValueSelector
,因此我假设这是您自己的代码或另一个库-在这种情况下,我无法真正帮助解释为什么该代码不起作用。但是,dotNetRDF已经有了一个方法
IGraph.GetTriplesWithPredicateObject
,可以满足您的需要。我已经更新了答案中的示例,以显示如何获取匹配的三元组列表。@Kal我认为这是从很早以前的一个旧的和不推荐的API中获得的versions@BünyaminAliYılmaz@Kal的回答试图表明根本不需要使用
HasPropertyValueSelector
,只需使用
GetTriplesWithPredicateObject()
方法。感谢您的努力和建议。我从上周开始学习DotNetRdf库。现在我最好读懂这个图书馆。GetTriplesWithPredicateObject方法对于选择任何节点或三元组都非常有用。再次感谢。