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; }
}
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;
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 = ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.