Error when setting typed variable in foreach

Hi all. I’m getting an error when I set a DateTime typed variable in a foreach where the array is empty. Any suggestions?

Error: Error! An explicit predefined conversion from type ‘System.Object’ to type ‘System.DateTime’ does not exist.

Data:

{Entities:[]} // Getting the error with this data

OR

{Entities: [
{"StartDate": "2023-09-19T02:03:55Z", "EndDate": "2024-09-19T02:03:55Z"},
{"StartDate": "2023-09-20T03:04:23Z", "EndDate": "2024-09-20T03:04:23Z"}
]}

Syntax:

<<foreach[ent in Entities]>>
<<var[DateTime date1 = ent.StartDate]>>
<<var[DateTime date2 = ent.EndDate]>>
<<if[date1.ToString("MMyyyy") == date2.ToString("MMyyyy")]>>
Dates are in the same month
<</if>>
<</foreach>>

@airbags Please try modifying your template like this:

<<foreach[ent in Entities]>>
<<var[date1 = ent.StartDate]>>
<<var[date2 = ent.EndDate]>>
<<if[date1.ToString("MMyyyy") == date2.ToString("MMyyyy")]>>
Dates are in the same month
<</if>>
<</foreach>>

And set ReportBuildOptions.AllowMissingMembers option. For example see the following code:

JsonDataSource data = new JsonDataSource(@"C:\Temp\data2.json");
Document doc = new Document(@"C:\Temp\in.docx");
ReportingEngine engine = new ReportingEngine();
engine.Options = ReportBuildOptions.AllowMissingMembers | ReportBuildOptions.RemoveEmptyParagraphs;
engine.BuildReport(doc, data, "Entities");
doc.Save(@"C:\Temp\out.docx");

@alexey.noskov thanks for the advice. Coupled with the advice you gave from a previous thread, I’ve come up with the following syntax that gets the start and end dates in the format our client requires for their reports and doesn’t error when an empty array is entered.

No need for the AllowMissingMembers as I believe this was an issue with the linq reporting engine trying to evaluate variables in the foreach loop and not being able to cast to not-nullable DateTime variable.

I’ve opted to cast in the conditions rather than at the variable declaration.

<<var[date1=ent.StartDate]>>
<<var[date2=ent.EndDate]>>
<<if[date1!=null && date2!=null]>>
<<if[((DateTime)date1).ToString("ddMMyyyy")==((DateTime)date2).ToString("ddMMyyyy")]>>
<<[date1]:"d MMM yyyy">>
<<else>>
<<if[((DateTime)date1).ToString("MMyyyy")==((DateTime)date2).ToString("MMyyyy")]>>
<<[date1]:"dd">>-<<[date2]:"d MMMM yyyy">>
<<else>>
<<if[((DateTime)date1).Year==((DateTime)date2).Year]>>
<<[date1]:"d MMMM">> - <<[date2]:"d MMMM yyyy">>
<<else>>
<<[date1]:"d MMMM yyyy">> - <<[date2]:"d MMMM yyyy">>
<</if>>
<</if>>
<</if>>
<</if>>

Output exampple
16 September 2023
16-19 September 2023
16 September - 12 October 2023
16 September 2023 - 21 February 2024

@airbags It is perfect that you managed to achieve what is required. optionaly insted of casting, you can use date1.Value.ToString("ddMMyyyy") syntax instead of casting nullable DateTime to DateTime. Please see the following updated syntax:

<<foreach[ent in Entities]>>
<<var[date1=ent.StartDate]>>
<<var[date2=ent.EndDate]>>
<<if[date1!=null && date2!=null]>>
<<if[date1.Value.ToString("ddMMyyyy")==date2.Value.ToString("ddMMyyyy")]>>
<<[date1]:"d MMM yyyy">>
<<else>>
<<if[date1.Value.ToString("MMyyyy")==date2.Value.ToString("MMyyyy")]>>
<<[date1]:"dd">>-<<[date2]:"d MMMM yyyy">>
<<else>>
<<if[date1.Value.ToString("yyyy")==date2.Value.ToString("yyyy")]>>
<<[date1]:"d MMMM">> - <<[date2]:"d MMMM yyyy">>
<<else>>
<<[date1]:"d MMMM yyyy">> - <<[date2]:"d MMMM yyyy">>
<</if>>
<</if>>
<</if>>
<</if>>
<</foreach>>

1 Like