如何初始化列表集合中的列表

How to initialize a list within a list collection

我有一个名为 Item 的 class,它包含一个 Airpricepoint 列表。我能够创建和初始化 class 的所有属性并添加 Airpricepoint 列表。在 Airpricepoint class 中,我有另一个列表 AirPricingInfo,它的属性很少。如何访问 AirPricingInfo 的成员、初始化它们并添加到列表中?

public class Item
{
    public List<Airpricepoint> AirPricePoint { get; set; }
}

public class Airpricepoint
{
    public  List<Airpricinginfo> AirPricingInfo { get; set; }
    public string AirPricingResultMessage { get; set; }
    public string FeeInfo { get; set; }
    public string FareNote { get; set; }
    public string TaxInfo { get; set; }
    public string Key { get; set; }
    public string TotalPrice { get; set; }
    public string BasePrice { get; set; }
    public string ApproximateTotalPrice { get; set; }
    public string ApproximateBasePrice { get; set; }
    public string EquivalentBasePrice { get; set; }
    public string Taxes { get; set; }
    public string Fees { get; set; }
    public string Services { get; set; }
    public string ApproximateTaxes { get; set; }
    public string ApproximateFees { get; set; }
    public string CompleteItinerary { get; set; }
}

public class Airpricinginfo
{
    public object FareInfo { get; set; }
    public object FareStatus { get; set; }
    public IList<FareInfoRef> FareInfoRef { get; set; }
    public object BookingInfo { get; set; }
    public IList<TaxInfo> TaxInfo { get; set; }
    public string FareCalc { get; set; }
}

var lowfaresearchres = new Lowfaresearchres();
lowfaresearchres.Items= new Item();
lowfaresearchres.Items.AirPricePoint = new List<Airpricepoint>();    
lowfaresearchres.Items.AirPricePoint.Add(new Airpricepoint()
    {
        ApproximateBasePrice = airPricePoint.ApproximateBasePrice,
        ApproximateTotalPrice = airPricePoint.ApproximateTotalPrice,
        ApproximateTaxes = airPricePoint.ApproximateTaxes,
        ApproximateFees = airPricePoint.Fees,
        BasePrice = airPricePoint.BasePrice,
        Taxes = airPricePoint.Taxes,
        TotalPrice = airPricePoint.TotalPrice
    });

我认为您要问的是两部分 - 如何用项目初始化列表(不调用 Add),以及如何初始化包含其他列表的列表(实际上是相同的答案).

初始化任何内联对象的语法是在 new ObjectType 之后使用大括号 {},在大括号内你有一个 PropertyName = Value 对的逗号分隔列表。

使用具有单一属性的 class 的简单形式是:

var person = new Person { Name = "Henry", Age = 25 };

使用单个列表的简单形式是:

var stringList = new List<string> { "one", "two", "three" };

var people = new List<Person>
{
    new Person {Birthday = DateTime.Now, Name = "Jack"},
    new Person {Birthday = DateTime.Parse("1/1/1980"), Name = "Jane"}
};

使用嵌套列表,它看起来像:

var listOfListOfStrings = new List<List<string>>
{
    new List<string>{ "one", "two", "three" },
    new List<string>{ "four", "five", "six" }
};

要填充您的对象,您可以通过几种方式完成。一种是先创建和初始化最里面的列表,然后在创建它们时将它们添加到父列表中:

// Create child lists
IList<FareInfoRef> fareInfoRef = IList<FareInfoRef>(); // And add some items
IList<TaxInfo> taxInfo  = new IList<TaxInfo>(); // And add some items

// Create the parent and add children
Airpricinginfo airPricingInfo = new Airpricinginfo 
{
    TaxInfo = taxInfo,
    FareInfoRef = fareInfoRef,
    // initialize other properties
}

// Continue with this pattern. . .

但我认为您要问的是,如何内联完成这一切(这可能看起来有点乱,但可能更简洁)。

所以将这个概念应用到你的对象中,你会得到类似于这个的东西(注意我没有编译这个,所以某处可能有一个小错误):

lowfaresearchres.Items.AirPricePoint = new List<Airpricepoint>
{ 
    new Airpricepoint
    {
        AirPricingInfo = new List<Airpricinginfo>
        {
            new Airpricinginfo
            {
                TaxInfo = new IList<TaxInfo>(),
                FareInfoRef = IList<FareInfoRef>()

                // Add other AirPricingInfo properties
            }, 
            // Add more Airpricinginfo objects separated by commas
        }

        // Add other Airpricepoint properties
    },
    // Add more Airpricepoint objects separated by commas
};