Can not iterate over an instance of type ‘System.Object’. ’

Hello,

using the Aspose.Word library and I’m trying to generate a report with the ReportingEngine class using the KnownTypes.
I get the same error as rubcs, System.InvalidOperationException: ‘An error has been encountered at the end of expression’ item2 in item.Value]> '. Can not iterate over an instance of type ‘System.Object’. ’

This is my template:
<< foreach [item in data.Fields] >> << foreach [item2 in item] >> << [item2] >> << / >> foreach << / >> foreach

It is possible to receive the example code sent by awais.hafeez, maybe it could solve my problem. Thank you

@fcampanale,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input document
  • Please create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.
  • Any additional steps that you think might be required to reproduce this issue on our end.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

Jamio.zip (9.4 KB)

In the zip files there are my console application, without lib Aspose.Word and in the Jamio.zip there is Template with LINQ.

@fcampanale,

We are working on your query and will get back to you soon.

@fcampanale,

I am afraid, as shown in following screenshot we are seeing compile time errors:

What are these namespaces that you are including in solution?

using openwork.Information;
using openwork.Data;

Please provide a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Thanks for your cooperation.

This application compiles correctly, the only errors are due to the absence of the Aspose.Word lib

@fcampanale,

It compiles now. But, it is now showing a new error at run-time i.e. when the control reaches at Line: 27 of Program.cs:

ArrayValue array = new ArrayValue();

The following error is thrown:

Hi,
the error received on the ArrayValue class is related to the fact that I sent you the library and not the source of the same, so you can not debug these classes. To reproduce the error, just run the solution sent.
Do you need to receive the source of our basic classes to investigate our error?
This could be problematic for us.
ConsoleApp1.zip (2.7 MB)

@fcampanale,

I am afraid, we are still observing the same run-time error as mentioned in my previous post. Will you please provide a simplified console app that produces the same (An error has been encountered at the end of expression’ item2 in item.Value]> ) exception without using the custom code i.e. ArrayValue array = new ArrayValue();. Please provide console app that only uses standard .NET classes/routines. Thanks for your cooperation.

Hi,

this is the console application that use a :NET object classes/routines and replicate the same error, for my template .doc.
DocAutomation.zip (1.0 MB)

@fcampanale,

We tested the scenario and managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-17947. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for your inconvenience.

Hi,

you have some news about the progress of issue WORDSNET-17947.

Kind regards

@fcampanale,

I am afraid, no. Your issue is currently pending for analysis and is in the queue. We will inform you via this thread as soon as this issue is resolved or any further updates are available. We apologize for your inconvenience.

@fcampanale,

Regarding WORDSNET-17947, we have completed the work on your issue and concluded to close this issue as ‘Not a Bug’. Please see the following analysis details:

LINQ Reporting Engine tries to be as close to C# compiler behavior as possible. So, the first thing to check is whether a similar code works in C# or not.

ArrayList list = new ArrayList();
Dictionary<string, object> dictionary = new Dictionary<string, object>();
Dictionary<string, object> fields = new Dictionary<string, object>
{
    { "campo2", "pippo" },
    { "campo3", "pluto" }
};
 
list.Add(fields);
dictionary.Add("campo1", list);
 
foreach (var item in dictionary.Values)
foreach (var item2 in item) ;

The above-mentioned code snippet generates a compile-time error saying that ‘Object’ is not an appropriate type to be used in a foreach loop. This is because values of the dictionary are declared to be of type ‘Object’ rather than of type implementing ‘IEnumerable’. So, C# compiler generates this error. LINQ Reporting Engine does the same thing by throwing a corresponding exception, so this behavior is expected.

Basically, there are two options to overcome this:

Option 1: Using the original template but modifying declaration of data objects

Document doc = new Document(@"Jamio.docx");
 
ReportingEngine engine = new ReportingEngine();
 
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
Dictionary<string, List<Dictionary<string, object>>> dictionary = new Dictionary<string, List<Dictionary<string, object>>>();
Dictionary<string, object> fields = new Dictionary<string, object>
{
    { "campo2", "pippo" },
    { "campo3", "pluto" }
};
 
list.Add(fields);
dictionary.Add("campo1", list);
 
engine.BuildReport(doc, dictionary, "data");
 
doc.Save(@"RPT_Jamio.docx", SaveFormat.Docx);

Option 2: Using the original declaration of data objects but modifying the template (see attachment Jamio Modified.zip (10.7 KB))

Document doc = new Document(@"Jamio Modified.docx");
 
ReportingEngine engine = new ReportingEngine();
 
ArrayList list = new ArrayList();
Dictionary<string, object> dictionary = new Dictionary<string, object>();
Dictionary<string, object> fields = new Dictionary<string, object>
{
    { "campo2", "pippo" },
    { "campo3", "pluto" }
};
 
list.Add(fields);
dictionary.Add("campo1", list);
 
// NOTE: Generic type identifiers are not supported by the engine, so we use non-generic counterparts here.
engine.KnownTypes.Add(typeof(IEnumerable));
engine.KnownTypes.Add(typeof(IDictionary));
 
engine.BuildReport(doc, dictionary, "data");
 
doc.Save(@"RPT_Jamio.docx", SaveFormat.Docx);

Hope, this helps.