You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Converting to JSON and accessing metadata

Entities passed to a transformer can be converted to JSON using the AsDocument method; there is also a possibility to access metadata for a specified object using MetadataFor method.

AsDocument - converting to JSON

public class Products_AsDocument : AbstractTransformerCreationTask<Product>
{
	public class Result
	{
		public RavenJObject RawDocument { get; set; }
	}

	public Products_AsDocument()
	{
		TransformResults = products => from product in products
						select new
						{
							RawDocument = AsDocument(product)
						};
	}
}

IList<Products_AsDocument.Result> results = session
	.Query<Product>()
	.TransformWith<Products_AsDocument, Products_AsDocument.Result>()
	.ToList();

MetadataFor - accessing metadata

public class Products_WithMetadata : AbstractTransformerCreationTask<Product>
{
	public class Result
	{
		public Product Product { get; set; }

		public string EntityName { get; set; }

		public string ClrType { get; set; }
	}

	public Products_WithMetadata()
	{
		TransformResults = products => from product in products
						let metadata = MetadataFor(product)
						select new
						{
							Product = product,
							EntityName = metadata.Value<string>("Raven-Entity-Name"),
							ClrType = metadata.Value<string>("Raven-Clr-Type")
						};
	}
}

IList<Products_WithMetadata.Result> results = session
	.Query<Product>()
	.TransformWith<Products_WithMetadata, Products_WithMetadata.Result>()
	.ToList();