2

I am trying to include a second foreach loop to pull values from a list within an object but I am having issues trying to do this.

Here is the code

private List<Opportunity> CreateOpportunities(Basket basket)
    {
        var opps = new List<Opportunity>();

        foreach (var orderLine in basket.OrderLines)
        {
            opps.Add(new Opportunity()
            **{**
                DelegateList = new Delegates
                {
                    foreach (var delegates in orderLine.DelegatesList)
                    {
                        FirstName = delegates.FirstName,
                        LastName = delegates.LastName,
                        Email = delegates.Email
                    }
                },

                CourseId = new CourseBooking
                {
                    Id = orderLine.CourseId
                },
                Quantity = orderLine.Quantity,
                EventId = new EventBooking
                {
                    Id = orderLine.EventId
                },
                EventItemDecimalPrice = orderLine.Price,

                TaxType = new TaxType
                {
                    Id = 1,
                    Name = "UK VAT",
                    Rate = orderLine.VatRate
                },
                RegionCode = orderLine.RegionId
            });
        }

        return opps;
    }

What I need to achieve is that the foreach loops in each orderLine and then within that orderLine there is a list class and a foreach to loop through this, pulling the value out and declaring against FirstName, LastName and Email

Currently its not letting me create the foreach and getting warnings saying it expects a ; or } before the foreach. Any help would be great.

Edit: Delegate Constructor:

public class Delegates
{
    [JsonProperty("first_name")]
    public string FirstName { get; set; }
    [JsonProperty("last_name")]
    public string LastName { get; set; }
    [JsonProperty("email")]
    public string Email { get; set; }
}

Edit: Opportunity (DelegateList Class)

public class Opportunity
{
    [JsonProperty("event")]
    public EventBooking EventId { get; set; }
    [JsonProperty("course")]
    public CourseBooking CourseId { get; set; }
    public int Quantity { get; set; }
    /// <summary>
    /// This is the price per person, NOT the total of the opportunity
    /// </summary>
    [JsonProperty("price")]
    public decimal EventItemDecimalPrice { get; set; }

    [JsonProperty("tax_type")]
    public TaxType TaxType { get; set; }

    [JsonIgnore]
    public string RegionCode { get; set; }

    [JsonProperty("delegates")]
    public List<Delegates> DelegateList { get; set; }

}
8
  • 2
    You should remove the semi colon ; from the inner for-each loop Commented May 8, 2018 at 9:50
  • You should create a Delegates constructor with orderLine as parameter. Commented May 8, 2018 at 9:51
  • 2
    The inner foreach is inside the object initializer. That doesn't make sense Commented May 8, 2018 at 9:51
  • 1
    Please could you show us the Delegates type? You can't write a foreach loop inside an object initializer like that. Commented May 8, 2018 at 9:53
  • 1
    Also I am not sure why you are initializing Delegates() twice and that too with a for-each in one of them and the other being empty. Commented May 8, 2018 at 9:55

2 Answers 2

2

I think the problem is you are putting a for-each loop inside the object initialization. You should move your inner for-each loop before you are initializing DelegateList. Something like-

foreach (var orderLine in basket.OrderLines)
{
    var firstName;
    var lastName;
    var email;

    List<DelegateList> delegates = new List<DelegateList>();

    foreach (var delegates in orderLine.DelegatesList)
    {
        delegates.Add(new Delegates{firstName = delegates.FirstName,lastName = delegates.LastName,email = delegates.Email});
    }

    opps.Add(new Opportunity()
    {//...

Then initialize the DelegateList with the List populated above in the inner loop-

DelegateList = delegates;
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this. The first variable is now saying var does not exist in the current context. I'm also having the issue where the var delegates in orderLine.DelegatesList isn't linking delegates to the List<OrderDelegate> DelegatesList and just defining it as a local variable therefore delegates.FirstName is error'ing with object does not contain definition
Also, would the foreach overwrite itself and only populate the last entry in the list as FirstName? The summary of the code is that there is 1 orderLine but potentially multiple delegates for 1 orderline so the opportunity should be including each orderline with delegates included linked to their own orderline
@PaulAlmond Sorry, the inner for-each should be just after the outer for-each. I have modified the code please check.
Yes it would override the value since the inner loop is not storing the data. It is just copying/instantiating the field variable. If you all the values, you can use a List collection to store them.
Okay thats fixed the first bug - it's now error'ing with implicilty type variables must be initialised and I also still have the issue about calling the DelegatesList object
|
1

Use Linq's Select :

opps.Add(new Opportunity()
{
    DelegateList = orderLine.DelegatesList
       .Select(orderLineDelegate => new Delegates
       {            
            FirstName = orderLineDelegate.FirstName,
            LastName = orderLineDelegate.LastName,
            Email = orderLineDelegate.Email
       })
       .ToList(),

    CourseId = ...

3 Comments

Thanks for the alternative suggestion - works up until newDelegates which is error'ing with "the name newDelegates does not exist in the current context"
Figured out that newDelegates should be new Delegates (missing space). delegates however is pointing to OrderDelegate delegates rather than OrderLine DelegateList and as such it can't find FirstName in the context of delegates
It was because i had not opened OrderDelegate as a public class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.