LINQ Reporting Engine - Date Comparison within an 'If' statement in Aspose Word Template

I’m trying to compare a date within an ‘If’ statement in an Aspose template created manually in Word.

The conditional block looks something like this:
<<if[ date == "30-05-2022"]>> SOME-TEXT << /if>>

While generating the document using the LINQ Reporting Engine in C#, I’m getting the following error:
Can not apply operator ‘==’ to operands of type System.Nullable1[System.DateTime]’ and ‘System.Int32’.’

Can someone please help me with writing this conditional statement correctly?

@MohitR

An expression within brackets should follow C# syntax rules, so you can use the same approach for comparing DateTime values as you do in pure C#, for example, date == new DateTime(2022, 5, 30). (By the way, you can check expressions in a C# IDE before using in a template). Please note that all types which identifiers are used in template expressions (such as DateTime) should be made known by LINQ Reporting Engine. Here is code showing all in one:

DocumentBuilder builder = new DocumentBuilder();
builder.Write("<<if [date == new DateTime(2022, 5, 30)]>>Matching<<else>>Not matching<</if>>");

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(DateTime));
engine.BuildReport(builder.Document, new DateTime(2022, 5, 30), "date");

Console.WriteLine(builder.Document.GetText().Trim());  // Prints "Matching."
1 Like

Thanks a lot. This cleared a few things up, but I’m still a little confused. I’ve got the date I need to compare with in a JSON file. In the template, I want to show a section only if the date value in that JSON is equal to 30-05-2022.

How would I add this condition within the template?

@MohitR

By default, JsonDataSource recognizes strings like “30-05-2022” as DateTime? values. For details, please check the end of Accessing JSON Data.

That is, you can use the same approach when comparing date-time values read from JSON. Please check the following code:

DocumentBuilder builder = new DocumentBuilder();
builder.Write("<<if [date == new DateTime(2022, 5, 30)]>>Matching<<else>>Not matching<</if>>");

const string json = "{ date: \"30-05-2022\" }";
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
JsonDataSource dataSource = new JsonDataSource(stream);

ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(DateTime));
engine.BuildReport(builder.Document, dataSource);

Console.WriteLine(builder.Document.GetText().Trim());  // Prints "Matching."