Adding a list into a root list

I have set up classes to store data in my code.

using System.Collections.Generic;

public class Email {
    public string User_Email { get; set; }
    public DateTime dateTime { get; set; }
}

public class Status {
    public string User_Status { get; set; }
    public DateTime dateTime {get; set; }
}

public class Collection_Detail {
    public string Bank_Name { get; set; }
    public string Acc_Name { get; set; }
    public string Acc_No { get; set; }
    public List<Status> return_Status { get; set; }
    public List<Email> email_Notification { get; set; }
}

I add data into a List of Emails
List<Email> email = new List<Email>();
email_Notifications.Add(new Email_Notification() {User_Email = 'toSender' , dateTime = '2020/10/10'});

And now, I want to put Email list into 'Collection_Detail'. I tried by using
List <Collection_Detail> details = new List <Collection_Detail> ();
details.add(new Collection_Details() {});

However, I am getting an error.

cannot intialize type with a collection initiaizer because it does not
implement ‘System.Collections.IEnumerable’

  1. Any idea how to put a list(s) into another list as a root list ? or
  2. How do I implement ‘IEnumerable’ into code above ?
  3. I tried to refer this microsoft doc but no luck.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I’m not sure if there’s something I’m missing but this should be as easy as:

// Create the list of email notifications
List<Email> emails = new List<Email>();
emails.Add(new Email_Notification() {User_Email = 'toSender' , dateTime = '2020/10/10'});

// Create the list of collection detail
List <Collection_Detail> details = new List <Collection_Detail> ();
details.Add(new Collection_Detail() 
{
    email_Notification = emails
});

There were a number of variable names and class names which did not match in your code. Hopefully I’ve caught all errors in the above example.

The one I’ve not corrected is setting a DateTime instance to a string – that wont work but I assume you already know that and how to fix it.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x