简体   繁体   English

C#中的ForEach循环问题

[英]Issue with ForEach Loop in C#

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. 我试图包括第二个foreach循环以从对象内的列表中提取值,但是在尝试执行此操作时遇到了问题。

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 我需要实现的是,在每个orderLine中都进行foreach循环,然后在该orderLine中有一个列表类和一个foreach来循环进行此操作,提取值并针对FirstName,LastName和Email进行声明

Currently its not letting me create the foreach and getting warnings saying it expects a ; 目前它不允许我创建foreach并得到警告说它期望一个; or } before the foreach. 或}在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) 编辑:机会(DelegateList类)

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. 我认为问题在于您正在对象初始化中放入for-each循环。 You should move your inner for-each loop before you are initializing DelegateList . 在初始化DelegateList之前,应该移动内部的for-each循环。 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

DelegateList = delegates;

Use Linq's Select : 使用Linq的Select:

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

    CourseId = ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM