@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.