Iterate List of objects in Transformation

joyanta sen asked on August 2, 2018 18:45

HI, I am writing an custom macros, which is returning a object that contain a List of objects as below.

    public class EventDetail : AbstractDataContainer<EventDetail>
{
    [RegisterColumn]
    public string EventType { get; set; }
        [RegisterColumn]
    public List<RegistrationContact> RegistrationContacts { get; set; }
  }

Now I am trying to iterate the list in the transformation as below. But it is not showing the result..

 {% If(eventDetail["RegistrationContacts"].ToList()!=null && eventDetail["RegistrationContacts"].ToList().Count){
              foreach (var evt in eventDetail["RegistrationContacts"].ToList()) { %} 
                  <p>{% evt.RegistrationContactName; %}<br>
                  <a href="mailto:{% evt.RegistrationContactEmail; %}">{% evt.RegistrationContactEmail; %}</a><br>
                  <a href="tel:{% evt.RegistrationContactPhone; %}">{% evt.RegistrationContactPhone; %}</a></p>
               {% }
              }
              #%}

Could you please let me know where am I doing wrong. Please help me out.

Thanks, Joyanta

Recent Answers


Peter Mogilnitski answered on August 2, 2018 19:19 (last edited on August 2, 2018 19:23)

Check the event log, it should tell you why your macro is failing. My guess .ToList() is crushing. There is not ToList() unless (see macro methods) you have it as custom macro. Although there is List that converts a list of objects to an ArrayList.

0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on August 3, 2018 19:37

Hi Peter, I tried but not working.

Thanks

0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on August 3, 2018 20:37

Hi, Could anyone please let me know how to iterate a List of object returned by macro in transformation. Its an urgent need in my project. Thanks.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 3, 2018 22:16 (last edited on August 3, 2018 22:24)

your macro should work just remove .ToList():

{% 
res = "";
if (eventDetail["RegistrationContacts"]!=null) {
  foreach (evt in eventDetail["RegistrationContacts"]) 
  { 
      res += "<p>" + evt.RegistrationContactName + "<br>"
  }
}  
return res;
@%}
0 votesVote for this answer Mark as a Correct answer

joyanta sen answered on August 3, 2018 22:20

Did that. But Didn't work. I resolve this by removing the following statement

eventDetail["RegistrationContacts"].Count

but don't know why Count is not working. If you can tell me any other way to find out the count that will be great.

Thanks.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 3, 2018 22:30 (last edited on August 3, 2018 22:47)

Count doesn't work because it is not implemented. ToList() and Count() do not work (AbstractDataContainer does not have them) , but you actually you dont need them to iterate. try the script above see if it works

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.