0

I'm apparently thinking about this incorrectly as I can't find similar questions or examples. I instantiate an object and want to add a list to that object, but have not been able to find the correct syntax. What is the correct way to add a list to an object?

List<business> businesses = (from j in db.business.Include("rooms") where j.status == "active" select j).ToList();

List<listing> listings = new List<listing>();

foreach(business b in businesses)
{
    listing l = new listing();
    l.id = b.id;
    l.title = b.title;
    listings.Add(l);
    List<listingrooms> listingrooms = new List<listingrooms>();
    List<listingamenities> listingamenities = new List<listingamenities>();

    foreach (rooms r in b.rooms)
    {
        listingrooms lr = new listingrooms();
        lr.id = r.id;
        lr.createDate = r.createDate;
        lr.doubleBeds = r.doubleBeds;
        lr.kingBeds = r.kingBeds;
        lr.numberCabins = r.numberCabins;
        lr.numberRooms = r.numberRooms;
        lr.numberSites = r.numberSites;
        lr.numberSuites = r.numberSuites;

        l.listingrooms.Add(lr);
    }
}

My classes...

public class listing
{    
    public string title { get; set; }
    public int id { get; set; }

    public virtual ICollection<listingrooms> listingrooms { get; set; }
}

public partial class listingrooms
{
    public Nullable<int> numberRooms { get; set; }
    public Nullable<int> numberSuites { get; set; }
    public Nullable<int> kingBeds { get; set; }
    public Nullable<int> doubleBeds { get; set; }
    public Nullable<int> numberCabins { get; set; }
    public Nullable<int> numberSites { get; set; }
    public System.DateTime createDate { get; set; }
    public int id { get; set; }

    public virtual listing listing { get; set; }
}
kara
  • 3,205
  • 4
  • 20
  • 34
tintyethan
  • 1,772
  • 3
  • 20
  • 44

1 Answers1

1

You don't initliaize your List:

public class listing
{    

    public listing() // Create a costructor
    {
        // Initialize your List
        listingrooms = new List<listingrooms>();
    }

    public string title { get; set; }
    public int id { get; set; }

    public virtual ICollection<listingrooms> listingrooms { get; set; }
}
kara
  • 3,205
  • 4
  • 20
  • 34