0

I have this class in C#:

   public class Init
       {
          public string First_name { get; set; }.
          public List<LeadLocations> Locations { get; set; }
        }
   public Init()
    {
        this.Locations = new List<LeadLocations>();

    }

 public class LeadLocations
{
    public string City { get; set; }
    public string State { get; set; }
    public string County { get; set; }
}

I need to fill Locations with values that I have in another list like:

        foreach (var item2 in AreaOfInterest.AreaOfInterests)
                {

                    foreach (var item in transactionInit.Locations)
                    {
                        item.City = item2.City;
                        item.County = item2.County;
                    }
           }

but it never goes to second foreach as it is empty. How I can initialize it to new object, anything I try is not working.

3
  • AreaOfInterest.AreaOfInterests is empty. Find out why. Commented Feb 1, 2020 at 0:45
  • 1
    you class is wrongly formatted . is it a clone of source ? or it's errors in posting question ? Commented Feb 1, 2020 at 0:47
  • You haven't supplied enough information to answer this question, it also contains a fair amount of ambiguity as to what is happening. For starters where is AreaOfInterest.AreaOfInterests initialized and populated. Does your break point get hit on this line foreach (var item in transactionInit.Locations) Commented Feb 1, 2020 at 2:03

2 Answers 2

1

If you are trying to add location to a list of locations, you need to create a new location object (based on the AreaOfInterests objects) and then add that to the list of locations.

// Initialize your Locations if its not already initialized. 
//   If its not initialized, you will get Object Reference Not Set to Instance of an Object.
transactionInit.Locations = new List<LeadLocations>();
foreach (var item2 in AreaOfInterest.AreaOfInterests)
{
    // For Each item2, create a new location then insert it in transactionInit.Locations.
    var leadLocation = new LeadLocation() {City = item2.City, County = item2.County};
    transactionInit.Locations.Add(leadLocation);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I believe I understood what you are intending to do. You could do the following.


transactionInit.Locations.AddRange(AreaOfInterest.AreaOfInterests
                                                .Select(x=> new Location
                                                        {
                                                          City = x.City,
                                                          County=x.County
                                                        });

In the above code, you are iterating over the AreaOfInterest.AreaOfInterests to create instances of Location and using the List.AddRange(IEnumerable<T>) to add them to the Locations property of transactionInit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.