• User Guide
  • API Documentation
  • Tools
  • Developer Guide
  • Support
  • User Guide
  • Going Deeper
  • Extension Methods
Show / Hide Table of Contents
  • Getting Started
    • Core Concepts
    • Hello World
    • Reading RDF
    • Writing RDF
    • Working with Graphs
    • Typed Values and Lists
    • Working with Triple Stores
    • Building SPARQL
    • Querying with SPARQL
    • Updating with SPARQL
    • Formats Supported
  • Going Deeper
    • Exceptions
    • Event Model
    • Equality and Comparison
    • Utility Methods
    • Extension Methods
    • Namespace Mapper
    • URI Factory
    • Node Factory
  • Storage API
    • Triple Store Integration
    • Servers API
    • Transactions API
  • Storage Providers
    • Allegro Graph
    • Blazegraph
    • Dataset Files
    • 4store
    • Fuseki
    • In-Memory
    • Sesame
    • SPARQL Query Endpoints
    • SPARQL Query and Update Endpoints
    • SPARQL Graph Store Protocol
    • Stardog
  • Advanced SPARQL
    • Result Formatting
    • SPARQL Datasets
    • Full Text Querying with SPARQL
    • Advanced SPARQL Operations
  • Ontology API
  • Inference and Reasoning
  • Dynamic API
  • Formatting API
  • Configuration API
    • Graphs
    • Triple Stores
    • Object Factories
    • Readers and Writers
    • SPARQL Endpoints (Deprecated)
    • SPARQL Clients
    • Query Processors
    • Update Processors
    • SPARQL Datasets
    • SPARQL Expression Factories
    • SPARQL Operators
    • SPARQL Optimisers
    • Full Text Query
    • Reasoners
    • Storage Providers
    • Static Options
    • Proxy Servers
  • Handlers API
  • JSON-LD API
    • Expansion
    • Compaction
    • Flattening
    • Framing
    • Processor Options
    • Error Handling
  • How-To Guides
    • Debug HTTP Communication
    • Debug SPARQL Queries
    • Load OWL
    • Load RDF from a File
    • Load RDF from the Web
    • Minimize Memory Usage
    • Reify Triples
  • Upgrading to dotNetRDF 3.0
    • Key Changes
    • Global Options Changes

Extension Methods

The library provides a number of extension methods that can be used to simplify some common tasks and marginally decrease the amount of code you have to write. These extension methods are located in several static class called Extensions, LiteralExtensions, GraphExtensions and TripleStoreExtensions in the VDS.RDF namespace so anywhere you reference VDS.RDF you have the option of using these methods.

Note

To avoid being over-long and dull, this document does not cover all of the extension methods available in these classes. Interested readers are encouraged to follow the links above to the class API docs.

Assert

The Assert(this IGraph g, INode subj, INode pred, INode obj) method is a shorthand way of asserting a single Triple in a Graph without having to instantiate a Triple object yourself e.g.


//Create a Graph and set it's Base URI
IGraph g = new Graph();
g.BaseUri = new Uri("http://example.org");

//Create some Nodes
IUriNode thisGraph = g.CreateUriNode();
IUriNode rdfType = g.CreateUriNode("rdf:type");
IUriNode example = g.CreateUriNode("http://example.org/Example");

//Assert a Triple using the Graph's Assert method
g.Assert(new Triple(thisGraph, rdfType, example));

//Assert a Triple using the extension method
g.Assert(thisGraph, rdfType, example);

Note that these methods of asserting are semantically identical, they both assert the same Triple in the Graph.

GetEnhancedHashCode

The GetEnhancedHashCode(this Uri u) method is used to generate Hash Codes for Uri objects. This method is used internally in favour of the Uri classes own GetHashCode() method since the .Net implementation doesn't account for fragement identifiers in computing hash codes.

In most applications this wouldn't matter since the fragment identifier is usually insignificant but in the case of the Semantic Web fragment identifiers are an important part of URIs. Therefore we provide our own hash code function which does use the fragment identifier in computing hash codes.

LoadFromFile, LoadFromUri, LoadFromEmbeddedResource and LoadFromString

These extension methods for IGraph instances all provide shortcuts for invoking the various static loader classes that can be used to load RDF from various common sources as detailed in the [Reading RDF]reading_rdf.md#reading-rdf-from-common-sources) documentation.

Retract

The Retract(this IGraph g, INode subj, INode pred, INode obj) method the partner of the Assert(...) extension method and like that method is simply a shorthand way of retracting a Triple without having to explicitly instantiate it e.g.

IGraph g = new Graph();

//Create some Nodes
IUriNode thisGraph = g.CreateUriNode();
IUriNode rdfType = g.CreateUriNode("rdf:type");
IUriNode example = g.CreateUriNode("http://example.org/Example");

//Retract a Triple using the Graph's Retract method
g.Retract(new Triple(thisGraph, rdfType, example));

//Retract a Triple using the extension method
g.Retract(thisGraph, rdfType, example);

Again both methods of retracting are semantically identical.

ToLiteral

The ToLiteral(...) methods are a whole family of methods which can be used to turn common .Net types into their equivalent ILiteralNode representations. Methods are provided for boolean, int, long, byte, sbyte, decimal, float, double, DateTime, DateTimeOffset, TimeSpan.

Some overloads may allow for control over the exact literal generated, for example the DateTime has an overload which allows specifying whether to preserve precisely i.e. include fractional seconds

WithSubject, WithPredicate and WithObject

The WithSubject(this IEnumerable<Triple>, INode Subject), WithPredicate(IEnumerable<Triple>, INode Predicate), WithObject(this IEnumerable<Triple>, INode Object) methods are used to filter any IEnumerable<Triple> to find only the Triples that have the matching Subject, Predicate or Object.

  • Improve this Doc
In This Article
  • Assert
  • GetEnhancedHashCode
  • LoadFromFile, LoadFromUri, LoadFromEmbeddedResource and LoadFromString
  • Retract
  • ToLiteral
  • WithSubject, WithPredicate and WithObject
Back to top Generated by DocFX