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

Passing parameters

Parameters can be passed to alter transformations by:

  • using AddTransformerParameter in session Query
  • using SetTransformerParameters in session DocumentQuery
  • filling TransformerParameters in IndexQuery that can be used in commands Query
  • passing parameters directly in various Get methods from commands

To access passed parameters from within a transformer, use one of the two available methods: Parameter or ParameterOrDefault. The difference between those two methods is that Parameter will throw if the parameter is not supplied, and ParameterOrDefault will use the default value.

public class Employees_BirthDay : AbstractTransformerCreationTask<Employee>
{
	public class Result
	{
		public string FirstName { get; set; }

		public string LastName { get; set; }

		public string BirthDay { get; set; }
	}

	public Employees_BirthDay()
	{
		TransformResults =
			employees =>
			from employee in employees
			// if no parameter is passed use 't' format
			let dateFormat = ParameterOrDefault("DateFormat", "t").Value<string>()
			select new
			{
				FirstName = employee.FirstName,
				LastName = employee.LastName,
				BirthDay = employee.Birthday.ToString(dateFormat)
			};
	}
}

IList<Employees_BirthDay.Result> results = session
	.Query<Employee>()
	.TransformWith<Employees_BirthDay, Employees_BirthDay.Result>()
	.AddTransformerParameter("DateFormat", "D")
	.ToList();