LINQ Reporting Engine: Null values and operators

Hi,
We are using the LINQ Reporting Engine in C# to generate Word documents based on a Word template.
Our parameter AMOUNT in the JSON object can have two values:

  • A number, for example 7000000
  • Null

The below shortcode works for an amount like 7000000, but not for null values.
<<[AMOUNT / 1000000]:"0.##">>
The error we get is:
Error! Can not apply operator ‘/’ to operands of type ‘System.String’ and ‘System.Int32’.

We tried with the below shortcode to resolve this, but it also doesn’t work.
<<[AMOUNT ?? 0 / 1000000]:"0.##">>

Working with if else also doesn’t work. The reporting engine always executes the else clause even if we go into the if.

How can we resolve this? Worst case I need to tackle this on JSON generation side, for example by replacing null with 0 but I am not really a fan of this.

@wfbe Could you please provide your sample template and datasource that will allow us to reoriduce th eproblem? I tested with the following simple data:

{
  "data": [
    { Amount: 70000 },
    { Amount: null }
  ]
}

the following template:

<<foreach [a in data]>>Value: “<<[a.Amount/100]:”0.##”>>”
<</foreach>>

and the following code:

JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json");
Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "data");
doc.Save(@"C:\Temp\out.docx");

And here is the produced output: out.docx (10.0 KB)

@alexey.noskov
You can reproduce it as follows.

{
  "data": {
    "AMOUNT": null
}
}

<<[AMOUNT / 1000000.00]:"0.##">>

If we provide a number, it works. But sometimes there is no number available and so it is null. The division operator is causing the issue.

@wfbe You can achieve this using a custom method. For example see the following code:

JsonDataSource ds = new JsonDataSource(dir + "data.json");
Document doc = new Document(dir + "in.docx");
ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(NumberConverter));
engine.BuildReport(doc, ds, "data");
doc.Save(dir + "out.docx");
public static class NumberConverter
{
    public static double AsDouble(double? value)
    {
        return value ?? DefaultValue;
    }

    public static double AsDouble(long? value)
    {
        return value ?? DefaultValue;
    }

    public static double AsDouble(string value)
    {
        return DefaultValue;
    }

    private const double DefaultValue = double.NaN;
}

The template in this cases should look like this:
Value: <<[NumberConverter.AsDouble(data.Amount) / 1000000]:"0.##">>

@alexey.noskov thanks this works.
Two questions:

  • What is the best way to show something else than “NaN” in the generated document?
  • Is there a way to implement this without such as complex shortcode? For the business that fills in the shortcode, this is complex to understand.

@wfbe

You can simply return another default value instead of NaN, for example you can use zero as a default value.

I am afraid, I do not see any other way to achieve this without custom method.

@wfbe You can also try using JsonSimpleValueParseMode.Strict. In this case the following code will work:

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.SimpleValueParseMode = JsonSimpleValueParseMode.Strict;
JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json", options);
Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, ds, "data");
doc.Save(@"C:\Temp\out.docx");

@alexey.noskov , this second option also seems to work. Thanks for the great help!

1 Like