Develop faster ReportMagic Reports using C#

Is your C# stronger than your RMScript? Mine is :slight_smile:

Fortunately, you can use the [Execute:] or [File.Execute:] macros to do all that complex data manipulation in C# using LINQ.

[Execute:]
Simply use C# inline in rmscript to quickly perform powerful analyses:

It’s easy to get and set variables by treating the context argument as a dictionary. There is a slight compilation overhead, but there is a performance benefit when processing large datasets.

[File.Execute:]
image

Prefer to edit your C# in an IDE? Simply use the [File.Execute:] macro to execute an external .cs file.

How is it possible get / set the properties of a jObject that is part of the passed context?

[Object: value=`{"timefilter": 7, environmentFilter : [ "*" ]}`, =>WidgetTemplateObject]
[Execute: code=`
	public static void Main(Context context)
	{
		/* Get local copy of context item(s) */
		var WidgetTemplateObject = context["WidgetTemplateObject"];

		/* Update properties as desired */
		WidgetTemplateObject["timeFilter"] = 30;
		var TypeInfo = WidgetTemplateObject.GetType();

		/* Replace original context item with updated copy */
		context["WidgetTemplateObject"] = WidgetTemplateObject;
		context["TypeInfo"] = TypeInfo.ToString();
	}`
]

It will show me that the object WidgetTemplateObject is of type Newtonsoft.Json.Linq.JObject but I cannot work out how to manipulate it in any meaningful way. I can return it unchanged, or return it as a string, but not modify any of the property values. I have tried multiple different ways, and so far all result in an error of some form. Any guidance would be appreciated… :slight_smile:

Two issues…

  1. Like all items in a Context, the context dictionary item for “WidgetTemplateObject” is upcast to an object, so you need to cast it as a JObject (see below).
  2. In ReportMagic 3.21 and below, the Execute macro does not have this Newtonsoft namespace available. This has been fixed in ReportMagic 3.22 (our ref: MS-20179
    “[Execute:] Add JObject and JArray support”).
[Object: value=`{"timeFilter": 7, environmentFilter : [ "*" ]}`, =>WidgetTemplateObject]
[Execute: code=`
	public static void Main(Context context)
	{
		/* Get local copy of context item(s) */
		var WidgetTemplateObject = context["WidgetTemplateObject"] as JObject;

		/* Update properties as desired */
		WidgetTemplateObject["timeFilter"] = 30;

		/* Replace original context item with updated copy */
		context["WidgetTemplateObject"] = WidgetTemplateObject;
	}`
]

–

Following this update:

edit: fixed your typo

1 Like

Thanks for the quick reply @David_Bond - that explains what I was seeing.

I look forward to 3.22.