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?
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."
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?
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.